struts2拦截器

package com.longyi.citicoa.domain.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.longyi.citicoa.domain.model.UserInfo;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
* <p>Title: AutoCheckInterceptor.java</p>
*
* <p>Description:权限检查拦截器类 </p>
*
* <p>Date: May 4, 2011</p>
*
* <p>Time: 11:50:48 AM</p>
*
* <p>Copyright: 2011</p>
*
* <p>Company: longing</p>
*
* @author 范辉
* @version 1.0
*
* <p>============================================</p>
* <p>Modification History
* <p>Mender: </p>
* <p>Date: </p>
* <p>Reason: </p>
* <p>============================================</p>
*/
public class AutoCheckInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = 1L;
private Logger log;

public AutoCheckInterceptor() {
log = Logger.getLogger(AutoCheckInterceptor.class);
}

// 权限检查方法
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation actioninvocation) throws Exception {
Map map = actioninvocation.getInvocationContext().getSession(); // 获得session对象
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
UserInfo user = (UserInfo) map.get("user");
if (user == null) {
log.error("您还没有登录,请登录后在进行此操作");
response.getWriter().print("<script>top.location.href='http://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/index.jsp"+"'</script>");
return null;
}
return actioninvocation.invoke();
}

}



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "struts-2.0.dtd" >
<struts>

     <!-- 系统struts2全局默认配置 begin-->
     <constant name="struts.locale" value="zh_CN" />
     <constant name="struts.i18n.encoding" value="UTF-8" />
     <constant name="struts.configuration.xml.reload" value="true" />
     <constant name="struts.multipart.maxSize" value="512000000" />
     <constant name="struts.url.http.port" value="8080" />
     <constant name="struts.ui.templateDir" value="WEB-INF/template" />
     <constant name="struts.ui.theme" value="simple" />
     <!-- 系统struts2全局默认配置 end-->

<package name="base" extends="struts-default,json-default">
   
    <!-- 系统拦截器配置  begin-->
    <interceptors>
       <!--自定义权限检查拦截器类 begin -->
       <interceptor name="autoCheck" class="com.longyi.citicoa.domain.util.AutoCheckInterceptor" />
   <!--自定义权限检查拦截器类 end -->

       <!-- struts2默认拦截器链配置 begin-->
   <interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="profiling" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<!--  interceptor-ref name="conversionError" /--> 
<interceptor-ref name="validation">
<param name="excludeMethods">
input,back,cancel,browse
</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">
input,back,cancel,browse
</param>
</interceptor-ref>
</interceptor-stack>
<!-- struts2默认拦截器链配置 end-->

<!-- 权限检查拦截器链 begin-->
<interceptor-stack name="myDefault">
    <!-- struts2默认拦截器链配置-->
<interceptor-ref name="defaultStack" />
<!--权限检查拦截器类  -->
<interceptor-ref name="autoCheck" />
</interceptor-stack>
<!-- 权限检查拦截器链 end-->

    </interceptors>
    <!-- 系统拦截器配置  end-->
   
    <!--系统默认拦截器链配置 begin -->
<default-interceptor-ref name="myDefault"/>
<!--系统默认拦截器链配置 end -->

<!-- 下面定义的结果对所有的Action都有效 -->
<global-results>
<result name="login" type="redirect">/index.jsp</result>
<result name="exception">/page/frame/error.jsp</result>
<result name="globalError">/page/frame/exception.jsp</result>
</global-results>
        <!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="exception"  />
<exception-mapping exception="java.lang.Throwable" result="exception"  />
</global-exception-mappings>
</package>

<include file="struts-bpm.xml" />

</struts>



不需要用拦截器的处理

<!-- 登录 -->
<action name="loginAction" class="com.longyi.citicoa.domain.action.LoginAction">
<result name="seccess" type="redirect">page/frame/main.jsp</result>
<result name="loginError">page/frame/loginError.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

你可能感兴趣的:(struts)