struts2-10动态方法调用和使用通配符定义action

动态方法的调用:
如果action中存在多个方法时,可以使用(!+方法名)调用指定的方法。如下:

package com.gz.action;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class HelloWorldAction {

    private String mes;
    private String userName;

    public String getMessage() {
        return mes;
    }
    public String execute() throws UnsupportedEncodingException {
        mes = "hello world!";
        this.userName = URLEncoder.encode("你好,世界!","UTF-8");//进行编码
        return "success";
    }
    public String addGlob() {
        return "message";
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

假如访问上面action的URL路径为:/struts/test/helloworld.action,要访问action中的addGlob()方法,可以这样调用:

/struts/test/helloworld!addGlob.action

如果不想使用动态方法调用,则可以通过常量struts.enable.DynamicMethodInvocation关闭动态方法调用。

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

使用通配符定义action:

<package name="hello" namespace = "/test" extends = "struts-default">
        <action name = "helloworld_*" class = "com.gz.action.HelloWorldAction" method = "{1}">
            <result name = "success">/WEB-INF/test/message.jsp</result>
        </action>
    </package>
public String execute() throws UnsupportedEncodingException {
        mes = "hello world!";
        this.userName = URLEncoder.encode("你好,世界!","UTF-8");//进行编码
        return "success";
    }
    public String addGlob() {
        return "message";
    }

访问addGlob()方法,可以通过这样的URL路径访问:/test/helloworld_addGlob.action

猜你喜欢

转载自blog.csdn.net/weixin_39660593/article/details/78782037