struts 中自定义action访问方法

struts中action类继承了ActionSupport  默认实现了execute()方法

struts.xml配置文件中

然后可以配置如下映射:

<package name ="ActionDemo" extends ="struts-default">   

    <action name ="HelloWorld" class ="tutorial.HelloWorld">   

        <result> /HelloWorld.jsp</result>   

    </action>   

</package>

 

 

public class TestAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private String helo;
    private String hehe;
    private String message;

    public String aliasAction() {  
        setMessage("自定义Action调用方法");  
        return SUCCESS;  
    }
    
    public String getHelo() {
        return helo;
    }

    public void setHelo(String helo) {
        this.helo = helo;
    }

    @Override
    public String execute() throws Exception {
        helo = "hello,world";
        hehe = "haha";
        return SUCCESS;
    }

    public void setHehe(String hehe) {
        this.hehe = hehe;
    }

    public String getHehe() {
        return hehe;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

在默认情况下,当请求HelloWorld.do发生时,Struts 2会根据struts.xml里的Action映射集(Mapping)实例化tutoiral.HelloWorld类,并调用其execute()方法。当然,我们可以通过以下两种方法改变这种默认调用,这个功能(Feature)有点类似Struts 1中的LookupDispathAction。

在sturts.xml中新建Action,并指明其调用的方法。

访问Action时,在Action名后加上"!xxx"(xxx为方法名)。

 

然后可以在sturts.xml中指定method来设置请求的函数名:

    <action name="AliasHelloWorld" class="  
    tutorial.HelloWorld" method="aliasAction">  
       <result>/HelloWorld.jsp</result>  
    </action>

也可以直接在URL地址栏中使用"!method"来设置请求的函数名:

    http://localhost:8080/Struts 2Test  
    /HelloWorld!aliasAction.action

 上面为第一种方法是通过在struts.xml文件中对应的action声明method,然后在对应的action中写对应的方法。

下面介绍第二种方式,大同小异

<struts>

  
    <package name="default" namespace="/" extends="struts-default">
      
        <action name="hello" class="com.action.TestAction" method="aliasAction">
            <result>/success.jsp</result>
            <result name="add">/add.jsp</result>
            <result name="update">/update.jsp</result>
        </action>
    </package>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>

 

TestAction的action为

public class TestAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    
    public String add(){
        
        return "add";
    }
    
    public String update(){
        return "update";
    }

    public String aliasAction() {  
        setMessage("自定义Action调用方法");  
        return SUCCESS;  
    }
    
    public String getHelo() {
        return helo;
    }

    public void setHelo(String helo) {
        this.helo = helo;
    }

    @Override
    public String execute() throws Exception {
        helo = "hello,world";
        hehe = "haha";
        return SUCCESS;
    }

 
    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

 

 

贴出请求:http://admin-pc:8080/StrutsDemo/hello!update.action

第二个与第一个的异同:都声明了方法,第一个声明的方法的返回值为SUCCESS,第二个声明的方法的返回值为返回的为struts.xml中

  <result name="add">/add.jsp</result> 的name相同。

 第二个struts.xml中必须有    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>方可使用

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/zhaoleigege/p/5848466.html