1.创建自定义拦截器类
package com.bos.web.intercepter;
import org.apache.struts2.ServletActionContext;
import com.bos.domain.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class ManagerIntercepter extends MethodFilterInterceptor {
/**
*判断用户是否登录,如果没有登录,只能访问用户登录界面,放行登录的方法
*
*/
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
User user = (User) ServletActionContext.getRequest().getSession().getAttribute("loguser");
if(user==null){
return "login";
}
return invocation.invoke();
}
}
2.配置拦截器类
<interceptors>
<interceptor name="myintercepter" class="com.bos.web.intercepter.ManagerIntercepter">
<param name="excludeMethods">dologin</param>
</interceptor>
<interceptor-stack name="mystack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="myintercepter"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myintercepter"></default-interceptor-ref>
<!-- 全局结果集定义 -->
<global-results>
<result name="login">/login.jsp</result>
</global-results>
<!-- 需要进行权限控制的页面访问 -->
<action name="page_*_*">
<result type="dispatcher">/WEB-INF/pages/{1}/{2}.jsp</result>
</action>
最后
以上就是唠叨蛋挞最近收集整理的关于struts2创建自定义拦截器的全部内容,更多相关struts2创建自定义拦截器内容请搜索靠谱客的其他文章。
发表评论 取消回复