This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Basic ActionMapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Basic ActionMapping" Watch "Basic ActionMapping" New topic
Author

Basic ActionMapping

Charles McGuire
Ranch Hand

Joined: Jan 18, 2005
Posts: 99
I am trying to learn Struts by designing a very basic app.

Consider the following mappings from the struts-config.xml file:



Simple. I don't have any ActionClasses at this point. Is that okay, or are ActionClasses required?

When I point my browser to the welcome page which has the tag in it, , I get this error:



I am very confused and this Ted Husted book isn't helping... much. I'm sure this is an easy question. Thanks in advance.


There's no place like 127.0.0.1
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Action classes are required if you need to forward to another page using action mapping in struts-config.xml

The execute() method of Action class returns the ActionForward object which represents the page to be forwarded to.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Charles,

If you wish to simply display a JSP without any processing, you should declare a global forward as follows:

<global-forwards>
<forward name="aboutWillow" path="/form/aboutWillow.jsp"
</global-forwards>

If you do this, the link you set up should work just fine.

However, in most cases, you want to do some processing before displaying a JSP. For example, if aboutWillow.jsp is displaying data from a database, the struts way to do this is to create an action to do the work of getting the data from the database or (more properly) call other objects to get data from the database. It should then place the data into a form bean and then forward to the jsp, which will read or manipulate the information in the form bean.

If you want to do it this way, here is an example.

To declare the form do as follows:

<form-beans>
<form-bean name="AboutWillowForm"type="forms.AboutWillowForm">
</form-bean>
</form-beans>

You then create a java class called forms.AboutWillowForm. It has to extend org.apache.struts.action.ActionForm. It's basically just a javaBean other than that, with fields and getters and setters for those fields. (example: private String foo; public String getFoo() { return foo;} ... etc.)

To declare the action do as follows:

<action-mappings>
<action name="AboutWillowForm" path="/aboutWillowInit" scope="request" type="actions.AboutWillowInitAction">
</action>
</action-mappings>

You then write actions.AboutWillowInitAction. It has to extend org.apache.struts.action.Action and must have an execute() method that will be called by the struts framework. Here is a sample class:

package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import forms.AboutWillowForm;

/**
* @version 1.0
* @author
*/
public class AboutWillowInitAction extends Action

{

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
AboutWillowForm aboutWillowForm = (AboutWillowForm) form;

try {

aboutWillowForm.setBar("Bar Bar");
aboutWillowForm.setFoo("Foo Foo");
request.setAttribute("AboutWillowForm", aboutWillowForm);

} catch (Exception e) {

// Report the error using the appropriate name and ID.
errors.add("name", new ActionError("id"));

}

// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.

if (!errors.isEmpty()) {
saveErrors(request, errors);

// Forward control to the appropriate 'failure' URI (change name as desired)
//forward = mapping.findForward("failure");

} else {

// Forward control to the appropriate 'success' URI (change name as desired)
forward = mapping.findForward("aboutWillow");

}

// Finish with
return (forward);

}
}

Note the statement:
forward = mapping.findForward("aboutWillow");
This uses the global forward I showed you how to set up at the beginning of this message to actually display the aboutWillow.jsp.

Within the jsp, you can access "foo" and "bar" using the <bean:write> tag or the <html:text> tags using property="foo" or property="bar".

This seems complicated, but believe me, when creating complex web applications it keeps things organized and prevents the chaos you find when you try to put too much logic in the JSPs.


Merrill
Consultant, Sima Solutions
Charles McGuire
Ranch Hand

Joined: Jan 18, 2005
Posts: 99
Hi Merrill,

Thanks for that great reply!

At your suggestion, I tried both the global forward and as a (?) detailed action mapping. The global forward worked fine. Cool!

As I am trying to learn this, I pressed my luck by taking it out of the global-fowards and into the action-mapping area. The action only to returns "Success", it does not work with any business objects or form beans. The idea is to add that functionality once the basics are down, and teach me a thing or two in the process.

I set up a generic action called "Relay" that always returns Success, no matter what. Then the action mapping was changed as follows:



Now I'm getting a MalformedURLException. Here is the error:



Thanks for helping a confused idiot (me) I'm getting there...!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm not entirely sure what is going on here, but I do believe that you should take out the redirect="true" attribute of the <foraward> tag. Try that and see if it fixes the problem.

Merrill
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

You are making good progress, Charles. The Struts learning curve starts very steep.

Let's take a look at your html:link tag.
Right now it has a forward attribute because you were referencing a global-forward. But you want it to reference an action. You can probably see where I'm going with this. (Yes, there is an action attribute that you can use instead of forward in the html:link.)

Also, Ted H. makes a reference in his book to a SuccessAction which does exactly what your Relay does. The only difference is that his forward "success" is all lower case, which is a pretty good practice in Struts for avoiding case-sensitive typos.
[ February 16, 2005: Message edited by: Marc Peabody ]

A good workman is known by his tools.
 
 
subject: Basic ActionMapping
 
Similar Threads
Cannot find bean in any scope
Cannot Retrieve Definition for Form Bean Null
Newbie to struts....help with ActionForward error
javax.servlet.ServletException:javax.servlet.jsp.JspException: Cannot find bean: "riga" in any scope
Need to retrieve values from database on page loads.