需要实现 com.opensymphony.xwork2.Preparable接口,继承ActionSupport类:
public class OrderActionMethod extends ActionSupport implements Preparable {
private String username;
@Override
public void prepare() throws Exception {
System.out.println(" method prepare start ... ");
}
@Override
public String execute() throws Exception {
System.out.println(" method execute start ... ");
return SUCCESS;
}
@Override
public void validate() {
System.out.println(" method validate start ... ");
}
public String prepareAddUser(){
System.out.println(" method prepareaAddUser start ... ");
return SUCCESS;
}
public String validateAddUser(){
System.out.println(" method validateAddUser start ... ");
return SUCCESS;
}
public String addUser(){
System.out.println(" method addUser start ... ");
return SUCCESS;
}
public String getUsername() {
System.out.println(" method getUsername start ... ");
return username;
}
public void setUsername(String username) {
System.out.println(" method setUsername start ... ");
this.username = username;
}
}
1、按照上面的执行addUser顺序为:
10:21:30,695 INFO [STDOUT] method prepareaAddUser start ...
10:21:30,695 INFO [STDOUT] method prepare start ...
10:21:30,695 INFO [STDOUT] method setUsername start ...
10:21:30,696 INFO [STDOUT] method validateAddUser start ...
10:21:30,696 INFO [STDOUT] method validate start ...
10:21:30,696 INFO [STDOUT] method addUser start ...
从结果可以 prepare方法名方法 --> prepare方法 -->set方法--> validate方法名方法 -->validate方法--> 方法;
2、请求执行execute默认方法或执行 localhost:8080//manage/orderActionMethod.action?username=s(也就是没有指定要执行的方法):
10:59:52,706 INFO [STDOUT] method prepare start ...
10:59:52,706 INFO [STDOUT] method setUsername start ...
10:59:52,706 INFO [STDOUT] method validate start ...
10:59:52,706 INFO [STDOUT] method execute start ...