用户是否登录拦截器:
package interceptor;
import action.LoginAction;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginInterceptor extends AbstractInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
if (LoginAction.class == invocation.getAction().getClass()) {
String name = invocation.invoke();
return name;
}
ActionContext con = invocation.getInvocationContext();
if (null == con.get("userInfo")) {
return Action.LOGIN;
}
return invocation.invoke();
}
}
LoginAction.java
package action;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import service.LoginService;
import service.impl.LoginServiceImpl;
import bean.Person;
import bean.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor;
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
private LoginService loginService = new LoginServiceImpl();
@Override
public String execute() throws Exception {
if (this.loginService.isLogin(username, password)) {
User user = new User ();
user.setUsername(username);
user.setPassword(password);
ActionContext.getContext().getSession().put("userInfo", user);
return SUCCESS;
}
return Action.INPUT;
}
//为什么不在validate验证登陆。
//一般在validate值是进行一些没有业务逻辑的验证,比如:验证用户的名字长度时候合法,
//日期时候合法。而登陆是有义务逻辑,不要放在validate中。
public void validate() {
System.out.println("validate execute!");
}
}
struts.xml
<interceptors>
<interceptor name = "theInterceptor1" class = "interceptor.TheInterceptor1">
<param name="test">zhangsan</param>
</interceptor>
<interceptor name = "theInterceptor2" class = "interceptor.TheInterceptor2"></interceptor>
<interceptor name = "theInterceptor3" class = "interceptor.TheInterceptor3"></interceptor>
<interceptor name = "loginInterceptor" class = "interceptor.LoginInterceptor"></interceptor>
<interceptor-stack name="myDefaultInterceptorStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name = "defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name = "myDefaultInterceptorStack"></default-interceptor-ref>
<global-results>
<result name="usernameInvalid" >/usernameInvalid.jsp</result>
<result name = "passwordInvalid">/passwordInvalid.jsp</result>
<result name = "login">/error.jsp</result>
</global-results>
自定义拦截栈。