struts2系列2:Struts2 Action(一)

          Posted on 2010-06-30 03:11 java小爬蟲 閱讀(2886) 評論(2)  編輯  收藏

            什么是Struts2 Action呢?

            實現(xiàn)了
            public String execute() throws Exception;
            的任何一個java類都可以被認為是Struts2 Action。

            注意點:
              
               1:方法名默認為execute,也可以自定義方法名。
              
               2:返回值應該為:success,none,error,input和login;

            Struts2 Action的實現(xiàn)方法?(三種)

            1:
          public class IndexAction1 {
              
          public String execute(){
                  
          return "success";
              }

          }

            2:
          import com.opensymphony.xwork2.Action;

          public class IndexAction2 implements Action{
              
          public String execute(){
                  
          return SUCCESS;
              }
              
          }

            3:
          import com.opensymphony.xwork2.ActionSupport;

          public class IndexAction3 extends ActionSupport {
              
          private static final long serialVersionUID = 1L;
              @Override
              
          public String execute(){
                  
          return ERROR;
              }

          }

          請參考:com.opensymphony.xwork2.Action接口,com.opensymphony.xwork2.ActionSupport類。
                  Action接口定義了Struts2 Action應該實現(xiàn)的方法和5個字符串常量作為方法的返回值。
                  ActionSupport類是Struts2提供的一個具有基本實現(xiàn)的父類,方便用戶的使用。



          視圖層的跳轉

          在struts-core jar包中打開struts-default.xml文件,它是一個struts2的默認配置文件。在里面可以找到:

            <result-types>
                      
          <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
                      
          <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
                      
          <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
                      
          <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
                      
          <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
                      
          <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
                      
          <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
                      
          <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
                      
          <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
                      
          <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
           
          </result-types>

          它說明了向視圖層跳轉時有10種方式:

              默認方式為dispatcher,不寫時就代表默認方式;
              redirectAction:Struts2 Action的跳轉;
              redirect: 與response.sendRedirect()相同;
              dispatcher:與 

          RequestDispatcher    forward(ServletRequest request, ServletResponse response)相同;



          Struts2 Action方法的調用(三種):

          1:在<action />中沒有定義method屬性,表示調用當前Action的execute();
          <action name="index3" class="com.action.IndexAction3">
                      
          <result name="error">
                          
          /hello3.jsp
                      
          </result>
                  
          </action>
          2: Wildcards
              As an application grows in size, so will the number of action mappings. Wildcards can be used to combine similar mappings into one more generic mapping.

            
           <action name="index1*" class="com.action.IndexAction1" method="{1}" >
                      
          <result name="success" type="redirectAction" >
                      index3
                      
          </result>
                  
          </action>
          method中的1代表name中的第一個*;
             
          3:Dynamic Method Invocation 

              Dynamic Method Invocation (DMI) will use the string following a "!" character in an action name as the name of a method to invoke (instead of execute). A reference to "Category!create.action", says to use the "Category" action mapping, but call the create method instead.
           <action name="index2!*" class="com.action.IndexAction2" method="{1}">
                      
          <result name="success">
                          
          /hello2.jsp
                      
          </result>
                  
          </action>
          method中的1代表name中的第一個*;


          實例:
          package com.action;

          public class IndexAction1 {
              
          public String msg(){
                  
          return "success";
              }

          }

          package com.action;

          import com.opensymphony.xwork2.Action;

          public class IndexAction2 implements Action{
              
          public String execute(){
                  
          return SUCCESS;
              }
              
          public String succ(){
                  
          return SUCCESS;
              }
              
          }
           
          package com.action;

          import com.opensymphony.xwork2.ActionSupport;

          public class IndexAction3 extends ActionSupport {
              @Override
              
          public String execute(){
                  
                  
          return ERROR;
              }

          }
          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC
              
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              
          "http://struts.apache.org/dtds/struts-2.0.dtd">

          <struts>
              
          <!-- 
              
          <constant name="struts.enable.DynamicMethodInvocation" value="false" />
              
          <constant name="struts.devMode" value="false" />

              
          <include file="example.xml"/>



              
          <package name="default" namespace="/" extends="struts-default">
                  
          <default-action-ref name="index" />
                  
          <action name="index">
                      
          <result type="redirectAction">
                          
          <param name="actionName">HelloWorld</param>
                          
          <param name="namespace">/example</param>
                      
          </result>
                  
          </action>
              
          </package>
               
          -->
              
          <constant name="struts.enable.DynamicMethodInvocation" value="false" />
              
          <constant name="struts.devMode" value="true" />
              
          <package name="default" namespace="/" extends="struts-default">
                  
          <action name="index1*" class="com.action.IndexAction1" method="{1}" >
                      
          <result name="success" type="redirectAction" >
                      index3
                      
          </result>
                  
          </action>
                  
          <action name="index2!*" class="com.action.IndexAction2" method="{1}">
                      
          <result name="success">
                          
          /hello2.jsp
                      
          </result>
                  
          </action>
                  
          <action name="index3" class="com.action.IndexAction3">
                      
          <result name="error">
                          
          /hello3.jsp
                      
          </result>
                  
          </action>
              
          </package>
              
          <!-- Add packages here -->

          </struts>
           
          測試:
          分別用以下URL,注意觀察和思考:
          http://localhost:8080/struts2_0200_action/index3
          http://localhost:8080/struts2_0200_action/index2!succ
          http://localhost:8080/struts2_0200_action/index2
          http://localhost:8080/struts2_0200_action/index1msg

          注意:
          <result name="error">
              
          /hello3.jsp
          </result>
          name代表Action方法的返回值,
          缺省代表success
          type代表Action向視圖跳轉時的跳轉類型,缺省代表plainText。



          Feedback

          # re: struts2系列2:Struts2 Action(一)  回復  更多評論   

          2010-06-30 07:39 by 儂本多情
          java旅程

          # re: struts2系列2:Struts2 Action(一)  回復  更多評論   

          2010-06-30 09:19 by @joe
          建議寫點大家使用中有問題的東西或者struts性能方面的東西。 只是建議

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 化德县| 盐津县| 思茅市| 芜湖县| 龙游县| 花莲县| 建瓯市| 大英县| 炉霍县| 手机| 海城市| 抚远县| 锦州市| 焉耆| 曲阜市| 当阳市| 奉贤区| 西贡区| 大关县| 云和县| 瑞丽市| 阜阳市| 连城县| 洱源县| 定兴县| 垦利县| 苍梧县| 勃利县| 荣成市| 高台县| 丹阳市| 沙洋县| 安岳县| 梧州市| 舟曲县| 仪陇县| 营山县| 南宁市| 黎平县| 康定县| 新化县|