生活随笔
收集整理的這篇文章主要介紹了
在struts2中配置自定义拦截器放行多个方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
源碼:
自定義的攔截器類:
| //自定義攔截器類:LoginInterceptor ; package com.java.action.interceptor; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class LoginInterceptor extends MethodFilterInterceptor { private static final long serialVersionUID = -5315714306081057062L; @Override protected String doIntercept(ActionInvocation invocation) throws Exception { //Logger log = LoggerFactory.getLogger(getClass()); HttpSession session = ServletActionContext.getRequest().getSession(); Object obj = session.getAttribute("boperator"); if(null != obj ){ //log.debug("Skipping Interceptor... Method [" + doIntercept(null) + "] found in exclude list."); return invocation.invoke(); }else{ //log.debug("Skipping Interceptor... Method [" + doIntercept(null) + "] found in exclude list."); return null; } } } |
?
在struts2.xml中配置:
| ?<!-- package標簽下 --> <package name="helloactionpkg" extends="struts-default" namespace="/"> <!-- 自定義 攔截器 --> <interceptors> <interceptor name="login" class="com.java.action.interceptor.LoginInterceptor"></interceptor> </interceptors> <!-- package標簽內容 ?標簽尾 --> ? <!-- action標簽下 --> <action name="hello_*" class="com.java.action.UserAction" method="{1}" > <!-- 配置攔截器 --> <interceptor-ref name="login"> <!-- param?標簽下 ? name="excludeMethods" ??放行多個方法 ? 方法名1,方法名2 ?用逗號隔開即可 ?--> <param name="excludeMethods">toLogin,login</param> </interceptor-ref> <!-- 由于使用了自定義攔截器,應再次加載使用框架默認攔截器 --> <interceptor-ref name="defaultStack"></interceptor-ref> <!-- action標簽內容 ?標簽尾 --> |
?
原因--源碼(部分):
| protected Set<String> excludeMethods = Collections.emptySet(); protected Set<String> includeMethods = Collections.emptySet(); public void setExcludeMethods(String excludeMethods) { this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods); } public static Set<String> commaDelimitedStringToSet(String s) { Set<String> set = new HashSet<String>(); String[] split = s.split(","); for (String aSplit : split) { String trimmed = aSplit.trim(); if (trimmed.length() > 0) set.add(trimmed); } return set; } |
轉載于:https://www.cnblogs.com/moly/p/6830020.html
總結
以上是生活随笔為你收集整理的在struts2中配置自定义拦截器放行多个方法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。