struts2_7_Action类中方法的动态调用

2023-02-14,,,

(一)直接调用方法(不推荐使用)

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()方法。

struts2_7_Action类中方法的动态调用的相关教程结束。

《struts2_7_Action类中方法的动态调用.doc》

下载本文的Word格式文档,以方便收藏与打印。