自定義攔截器
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;
}
}