J F W

making web application programming easier is possible

HOW TO : CORE

A simple action

An action is basically a Java class (that extends ProjectAction) that receives and manages HTTP requests, and redirects the control flow to another resource. We will show an example of an action that just redirects the user to another page.
First of all, the server has to know when the user clicks a link or submits a form, what corresponding action to invoke. This information is stored in the struts-config.xml file. More precisely, in the action element.

In the example above, we have mapped an action, the attribute path declares where the resource is located inside the server, the attribute type indicates the Java class (with fully qualified name) associated with the resource. Below we can see an implemented action that does nothing.

Now we have to decide what the possible results of the action are. As mentioned before, we are just redirecting the user to another page, so that will be the only result possible. And this result has to be declared in the struts-config.xml file as a forward element, child of the associated action. That said, our declaration of the action will be changed as follows.

The name attribute of the just added forward element is the logical name we will use in the action. The path is a resource held by the server and target of the forwarding, if the result matches the name attribute. In our case the target of the forwarding is a jsp page but it could be anything else: a static html file or even another action.
What is left to do now is to implement in the action class, the method myExecute, to return an appropriate ActionForward, to actually forward the user to the page declared into the forward element in the struts-config.xml file. The modified class is shown below.


In the previous code we can see how an action actually uses a forward element "calling" it by its name attribute. In this way, any number of forward elements can be declared and called based on the result of the action. In this case, the result is just forwarding the user to another page.  In page home_tile.jsp we define the tiles skeleton to use, and the jsp page that contain the content to show. A little explanation for tiles can be found here.