生活随笔
收集整理的這篇文章主要介紹了
在struts2中配置自定义拦截器放行多个方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
源碼:
自定義的攔截器類:
| //自定義攔截器類: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標(biāo)簽下 --> <package name="helloactionpkg" extends="struts-default" namespace="/"> <!-- 自定義 攔截器 --> <interceptors> <interceptor name="login" class="com.java.action.interceptor.LoginInterceptor"></interceptor> </interceptors> <!-- package標(biāo)簽內(nèi)容 ?標(biāo)簽尾 --> ? <!-- action標(biāo)簽下 --> <action name="hello_*" class="com.java.action.UserAction" method="{1}" > <!-- 配置攔截器 --> <interceptor-ref name="login"> <!-- param?標(biāo)簽下 ? name="excludeMethods" ??放行多個(gè)方法 ? 方法名1,方法名2 ?用逗號(hào)隔開(kāi)即可 ?--> <param name="excludeMethods">toLogin,login</param> </interceptor-ref> <!-- 由于使用了自定義攔截器,應(yīng)再次加載使用框架默認(rèn)攔截器 --> <interceptor-ref name="defaultStack"></interceptor-ref> <!-- action標(biāo)簽內(nèi)容 ?標(biāo)簽尾 --> |
?
原因--源碼(部分):
| 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; } |
轉(zhuǎn)載于:https://www.cnblogs.com/moly/p/6830020.html
總結(jié)
以上是生活随笔為你收集整理的在struts2中配置自定义拦截器放行多个方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。