现在的位置: 首页 > 综合 > 正文

使用通配符定义Action

2018年06月09日 ⁄ 综合 ⁄ 共 928字 ⁄ 字号 评论关闭

使用通配符定义Action

在开发中通常使用通配符的方法来定义Action,定义一个ActionHelloWorld的类,如:

package zjh.struts2.lx;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

private String message;

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public String execute() {

message = "这是第一个方法";

return SUCCESS;

}

public String other_method(){

message = "这是第二个方法";

return SUCCESS;

}

}

Action定义好后,只需要在struts.xml配置文件中的action加个通配符(*)即可,如:

<package name="test" namespace="/test" extends="struts-default">

<action name="HelloWorld_*" class="zjh.struts2.lx.HelloWorld" method="{1}">

<result name="success">/success.jsp</result>

</action>

</package>

此时 ” HelloWorld_* ”表示的是匹配所有以“HelloWorld”开头的Action,method中的{1}代表的是*的内容,即如果Action为“HelloWorld_other_method”则{1}代表的就是“other_method”。如果这时要访问Action中的other_method()方法,可以通过这样的URL访问:

http://localhost:8080/struts/test/HelloWorld_other_method.action

抱歉!评论已关闭.