Академический Документы
Профессиональный Документы
Культура Документы
In this lesson I will show you how to use Struts Action Class and forward a jsp file
through it.
An Action works as an adapter between the contents of an incoming HTTP request and
the business logic that corresponds to it. Then the struts controller (ActionServlet) slects
an appropriate Action and creates an instance if necessary, and finally calls execute
method.
To use the Action, we need to Subclass and overwrite the execute() method. In the
Action Class don't add the business process logic, instead move the database and business
process logic to the process or dao layer.
The ActionServlet (commad) passes the parameterized class to Action Form using the
execute() method. The return type of the execute method is ActionForward which is used
by the Struts Framework to forward the request to the file as per the value of the returned
ActionForward object.
Our Action class (TestAction.java) is simple class that only forwards the TestAction.jsp.
Our Action class returns the ActionForward called "testAction", which is defined in the
struts-config.xml file (action mapping is show later in this page). Here is code of our
Action Class:
TestAction.java
package roseindia.net;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class TestAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
return mapping.findForward("testAction");
}
}
Action Class process the specified HTTP request, and create the corresponding HTTP
response (or forward to another web component that will create it), with provision for
handling exceptions thrown by the business logic. Return an ActionForward instance
describing where and how control should be forwarded, or null if the response has
already been completed.
Parameters:
Following code under the <action-mappings> tag is used to for mapping the TestAction
class.
<action
path="/TestAction"
type="roseindia.net.TestAction">
<forward name="testAction"
path="/pages/TestAction.jsp"/>
</action>
To test the new application click on Test the Action link on the index page. The content
of TestAction.jsp should be displayed on the user browser.
In this lesson you learned how to create Action Class and add the mappings in the struts-
config.xml. Our Action Class returns the ActionForward mapping of the TestAction.jsp.