自定义拦截器
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
//配置不拦截登陆方法
//拦截登陆以外的所有方法
public class LoginInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//校验是否登陆
//1 获得session
Object object = ActionContext.getContext().getSession().get("user");
//2 获得登陆标识
//3 判断标识是否存在
if(object != null){
//存在=>放行
return invocation.invoke();
}
//不存在=>拦截,重定向到登陆页面
return "toLogin";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 开启开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<package name="crm" namespace="/" extends="struts-default" >
<interceptors>
<!-- 注册拦截器 -->
<interceptor name="loginInterceptor" class="cn.cdw.web.interceptor.LoginInterceptor"></interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor">
<!-- 指定不拦截登陆(login)方法 -->
<param name="excludeMethods">login</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 指定默认拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<!-- 配置全局结果集 -->
<global-results>
<result name="toLogin" type="redirect" >/login.jsp</result>
</global-results>
<!-- 全局异常映射 -->
<global-exception-mappings>
<!-- 当出现java.lang.RuntimeException异常时,转发到error结果 -->
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<action name="CustomerAction_*" class="cn.cdw.web.action.CustomerAction" method="{1}" >
<result name="list" >/jsp/customer/list.jsp</result>
<!-- 重定向Action -->
<result name="toList" type="redirectAction" >
<param name="actionName">CustomerAction_list</param>
<param name="namespace">/</param>
</result>
</action>
<action name="UserAction_*" class="cn.cdw.web.action.UserAction" method="{1}" >
<result name="toHome" type="redirect" >/index.jsp</result>
<result name="login" >/login.jsp</result>
<result name="error" >/login.jsp</result>
</action>
</package>
</struts>
UserAction.java
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.cdw.domain.User;
import cn.cdw.service.CustomerService;
import cn.cdw.service.UserService;
import cn.cdw.service.impl.UserServiceImpl;
public class UserAction extends ActionSupport implements ModelDriven<User> {
private User user = new User();
//登陆方法
public String login() throws Exception {
//1 获得servletContext对象
ServletContext sc = ServletActionContext.getServletContext();
//2 调用工具方法从sc中获得applicationContext容器
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
//3 从容器中获得对象
UserService us = (UserService) ac.getBean("userService");
//----------------------------------------------------------------------------------
//1 调用Service执行登陆逻辑
User existU = us.login(user);
//2 将返回的User对象放入session域中
ActionContext.getContext().getSession().put("user", existU);
//3 重定向到项目首页
return "toHome";
}
@Override
public User getModel() {
return user;
}
}