Struts Action Class (II)

16 January 2008

This post is in continuation to Struts Action Class(I). Please go through it first.

Developing our Action Class

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

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");
  }
}

Understanding Action Class

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.

Adding the Action Mapping in the struts-config.xml

To test the application we will add a link in the index.jsp

<html:link page="/TestAction.do">Test the Action</html:link>

Following code under the tag is used to for mapping the TestAction class.

   <action
      path="/TestAction"
      type="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.

del.icio.us:Struts Action Class (II)  digg:Struts Action Class (II)  spurl:Struts Action Class (II)  wists:Struts Action Class (II)  simpy:Struts Action Class (II)  newsvine:Struts Action Class (II)  blinklist:Struts Action Class (II)  furl:Struts Action Class (II)  reddit:Struts Action Class (II)  fark:Struts Action Class (II)  blogmarks:Struts Action Class (II)  Y!:Struts Action Class (II)  smarking:Struts Action Class (II)  magnolia:Struts Action Class (II)  segnalo:Struts Action Class (II)  gifttagging:Struts Action Class (II)

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Struts Action Class (II)

Leave a Reply