自定义struts2拦截器

首先我们需要定义 一个拦截器

里面实现的代码很少 主要在strts.xml文件配置里

protected String doIntercept(ActionInvocation invocation) throws Exception {
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		
		if(user == null){
			return "login";
		}
		
		return invocation.invoke();
	}

struts配置  定义一个全局结果
<!--配置拦截器  -->
		<interceptors>
			<interceptor name="BosLoginInterceptor" class="com.cb.bos.web.interceptor.BosLoginInterceptor">
				<param name="excludeMethods">login</param>
			</interceptor>
			
			<interceptor-stack name="mystack">
				<interceptor-ref name="BosLoginInterceptor"/>
				<interceptor-ref name="defaultStack"/>
			</interceptor-stack>	
		</interceptors>
		<!--指定默认栈  -->
		<default-interceptor-ref name="mystack"/>
	
	<global-results>
		<result name="login">/login.jsp</result>
	</global-results>


猜你喜欢

转载自blog.csdn.net/fly_eason/article/details/77236031