PreResultListener是一个监听器接口,它可以在Action完成控制处理之后,系统转入实际的物理视图之间被回调。
package com.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import com.po.User;
public class LoginAction extends ActionSupport{
private User user;
public String execute() throws Exception {
System.out.println("我是被拦截的方法");
ActionInvocation invocation = ActionContext.getContext()
.getActionInvocation();
invocation.addPreResultListener(new PreResultListener() {
public void beforeResult(ActionInvocation invocation,
String resultCode) {
System.out.println("PreResultListener中的方法执行");
System.out.println(resultCode);
}
});
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession().setAttribute("user", user);
System.out.println("执行完毕");
return "success";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void validate() {
if(user.getName().trim().equals(""))
{
this.addActionError(getText("username.required"));
}
else if(user.getPassword().trim().equals(""))
{
this.addActionError(getText("password.required"));
}
}
}