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

struts2_7_Action类中方法的动态调用

2018-01-23 10:54 344 查看
(一)直接调用方法(不推荐使用)

1)Action类:

private String savePath;

public String getSavePath() {
return savePath;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

public String other() {
savePath = "other";
return "success";
}

public String execute() {
savePath = "execute";
return "success";
}


2)struts.xml文件的配置:

<struts>
<package name="package" namespace="/test"
  extends="struts-default">
<action name="emp" class="struts.employeeAction"
  method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>


当输入:http://localhost:8080/Struts_3/test/emp.action

时会输出:execute 即调用execute()方法;

当输入:http://localhost:8080/Struts_3/test/emp!other.action

时会输出:other 即调用other()方法。

(二)使用通配符(推荐使用)

1)Aciton类与(一)中的同样

2)struts.xml文件的配置:

<struts>
<package name="package" namespace="/test"
  extends="struts-default">
<action name="emp*" class="struts.employeeAction"
  method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>

訪问路径:http://localhost:8080/Struts_3/test/empexecute

这时会输出:execute 即调用execute()方法;

訪问路径:http://localhost:8080/Struts_3/test/empother

这时会输出:other 即调用other()方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: