<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    struts2系列2:Struts2 Action(一)

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

      什么是Struts2 Action呢?

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

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

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

      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應該實現的方法和5個字符串常量作為方法的返回值。
            ActionSupport類是Struts2提供的一個具有基本實現的父類,方便用戶的使用。



    視圖層的跳轉

    在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性能方面的東西。 只是建議

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


    網站導航:
     
    主站蜘蛛池模板: 免费看国产精品3a黄的视频| 日批日出水久久亚洲精品tv| 最近2019免费中文字幕6| 国产黄在线播放免费观看| 成年免费a级毛片免费看无码| 免费一级毛片无毒不卡| 91人人区免费区人人| 日韩在线免费电影| 国产亚洲情侣一区二区无码AV| 亚洲国产精品无码久久SM| 亚洲欧洲精品久久| 免费无遮挡无码视频在线观看| 在线播放免费人成毛片乱码| 免费无码又爽又刺激聊天APP| 亚洲男女内射在线播放| 亚洲成AV人综合在线观看| 真人无码作爱免费视频| 曰批全过程免费视频播放网站| 日本免费人成黄页网观看视频| 亚洲AV日韩AV永久无码免下载| 亚洲av日韩综合一区久热| 久久国产色AV免费看| 亚洲第一页日韩专区| 亚洲一卡2卡三卡4卡无卡下载| 免费在线观影网站| 久久亚洲国产成人精品无码区| 亚洲AV成人一区二区三区观看| 亚欧免费无码aⅴ在线观看| 国产亚洲精品福利在线无卡一| 亚洲成AV人片高潮喷水| 成人免费视频小说| 亚洲福利秒拍一区二区| 男女作爱在线播放免费网站| 久久精品亚洲乱码伦伦中文| 免费观看又污又黄在线观看| 免费看片A级毛片免费看| 亚洲av永久无码精品秋霞电影秋 | 日韩亚洲人成网站| 扒开双腿猛进入爽爽免费视频| 中文文字幕文字幕亚洲色| 亚欧人成精品免费观看|