JAVA核心框架
Struts | Spring | MyBatis |
---|
Action的动态调用方法
标签: action 动态调用方法 it 分类: JAVA
Action执行的时候并不一定要执行execute方法,我们可以指定Action执行哪个方法:
`
/user_add_success.jsp
这样,只要在action的对象中有一个add的方法,并且返回类型为String就可以了。如果没有method属性,则默认执行execute()方法。
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String add() {
return SUCCESS;
}
}
2、 动态方法调用DMI(推荐)
可以在url地址中动态指定action执行那个方法。Url地址如下:
http://localhost:8080/Struts2_0500_ActionMethod/user/user!add
方法:action + ! + 方法名
注:只要Action对象中有这个方法,并且返回类型为String就可以调用。
这样Struts.xml配置文件中不需要配置methed属性。代码如下:
/user_add_success.jsp
Action类:
public class UserAction extends ActionSupport {
public String add() {
return SUCCESS;
}
}
总结:推荐使用第二种动态方法调用DMI,因为第一种需要大量的Action配置,后者可以在url中动态指定执行action中哪个方法。