您的位置:首页 > 编程语言 > Java开发

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

2017-12-12 15:22 721 查看
动态方法的调用:

如果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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: