动态调用Action中的方法
如果Action中存在多个方法时,可以使用!+方法名调用指定方法。如下:
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中的other_method()方法,可以这样调用,即:
http://localhost:8080/struts/test/HelloWorld!other_method.action |
如果不想使用动态方法调用,还可能通过常量struts.enable.DynamicMethodInvocation关闭动态方法的调用。语句为:
<constant name = “struts.enable.DynamicMethodInvocation” value = “false”/> |
静态调用Action中的方法
除了可以使用动态方法调用Action中的方法,还可以通过struts.xml配置文件的方法静态调用Action中的方法,只需要将struts.xml配置文件中action中的method的值改为需要调用的方法即可,如:
<package name="test" namespace="/test" extends="struts-default"> <action name="HelloWorld" class="zjh.struts2.lx.HelloWorld" method = “other_method”> <param name="message">为action属性注入值</param> <result name="success">/success.jsp</result> </action> </package> |
以上方法调用了Action为HelloWorld中的other_method()方法。