期待更好更穩定的開源FrameWork的出現,讓我們一起努力吧!  
          日歷
          <2007年6月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567
          統計
          • 隨筆 - 78
          • 文章 - 1
          • 評論 - 29
          • 引用 - 0

          導航

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案(42)

          文章檔案(37)

          相冊

          搜索

          •  

          積分與排名

          • 積分 - 45228
          • 排名 - 1064

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

           

          In this lesson I will show you how to use Struts Action Class and forward a jsp file through it. 

          What is Action Class?

          An Action class in the struts application extends Struts 'org.apache.struts.action.Action" Class. Action class acts as wrapper around the business logic and provides an inteface to the application's Model layer. It acts as glue between the View and Model layer. It also transfers the data from the view layer to the specific business process layer and finally returns the procssed data from business layer to the view layer.

          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.

          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

          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 responsethrows Exception{
                return mapping.findForward("testAction");
            }
          }

             
          Understanding Action Class
          Here is the signature of the Action Class.

          public ActionForward execute(ActionMapping mapping,
          ActionForm form,
          javax.servlet.http.HttpServletRequest request,
          javax.servlet.http.HttpServletResponse response)
          throws java.lang.Exception

          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:

          mapping - The ActionMapping used to select this instance
          form - The optional ActionForm bean for this request (if any)
          request - The HTTP request we are processing
          response - The HTTP response we are creating
          Throws:
          Action class throws java.lang.Exception - if the application business logic throws an exception

          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 <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.

                                   



          posted on 2007-06-07 21:09 BlueSky_itwangxinli 閱讀(500) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          Copyright © BlueSky_itwangxinli Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 班戈县| 绍兴县| 武山县| 凤城市| 永善县| 布拖县| 汽车| 屯留县| 长春市| 井陉县| 香河县| 尚义县| 松潘县| 花垣县| 宜兴市| 东宁县| 河间市| 邵阳市| 临湘市| 景谷| 虞城县| 永靖县| 调兵山市| 永济市| 台前县| 高陵县| 长春市| 天全县| 墨竹工卡县| 连平县| 宁晋县| 延吉市| 宿松县| 深圳市| 古丈县| 嘉禾县| 含山县| 页游| 元谋县| 通城县| 惠安县|