基于全注解的ssh的拦截器的写法

最近在用ssh框架写一个项目,是基于全注解的,上网搜了搜,这才发现全注解的struts的拦截器跟基于xml的拦截的写法略有不同,在这里小结一下,以备以后在写的时候忘了该怎么用。

 

 

 

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.convention.default.parent.package"
                value="crud-default" />
      <!--   一定要继承自convention-default -->
        <package name="crud-default" extends="convention-default" namespace="/">
                <interceptors>
                 <!--    你的拦截器 -->
                        <interceptor name="checklogin" class="com.kaishengit.interceptor.LoginInterceptor"></interceptor>
                        

                    <!--     基于paramsPrepareParamsStack, 增加store interceptor保证actionMessage在redirect后不会丢失 -->
                        <interceptor-stack name="crudStack">
                                <interceptor-ref name="store">
                                        <param name="operationMode">AUTOMATIC</param>
                                </interceptor-ref>
                                <interceptor-ref name="paramsPrepareParamsStack" />
                             <!--    在拦截器栈里引入你所加的拦截器 -->
                                <interceptor-ref name="checklogin">
                                        <param name="loginUser">user</param>
                                        <param name="exclude">tiaozhuan</param>
                                       
                                </interceptor-ref>
                        </interceptor-stack>

                </interceptors>
                <default-interceptor-ref name="crudStack" />

               <global-results>
	             <result name="login" type="redirectAction">
	             		<param name="actionName">tiaozhuan</param>
	             		<param name="namespace">/</param>
	             </result>     
	    	</global-results>
		</package>
	 
</struts>

 

 

 

LoginInterceptor.java

 

 

package com.kaishengit.interceptor;

import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.TextParseUtil;

public class LoginInterceptor extends AbstractInterceptor{
			
	private static final long serialVersionUID = 1L;
	
	 private String loginUser;//session 中放的值;
     private String exclude;//不需要拦截的页面;

     @Override
     public String intercept(ActionInvocation invocation) throws Exception {
             Set<String> set = TextParseUtil.commaDelimitedStringToSet(exclude);
             System.out.println(invocation.getProxy().getActionName());
             if (set.contains(invocation.getProxy().getActionName())) {
                     return invocation.invoke();
             } else {
                     Map<String, Object> session = invocation.getInvocationContext().getContext().getSession();
                                     
                     if (session.get(loginUser) != null) {
                             return invocation.invoke();
                     }
             }
             return "login";
     }

     public String getLoginUser() {
             return loginUser;
     }

     public void setLoginUser(String loginUser) {
             this.loginUser = loginUser;
     }

     public String getExclude() {
             return exclude;
     }

     public void setExclude(String exclude) {
             this.exclude = exclude;
     }


}
 

你可能感兴趣的:(struts,ssh,Interceptor)