struts2拦截器配置

在struts2中有自己默认的拦截器,但是在很多时候我们需要一个自己的拦截器去处理例如:用户权限控制,用户登录控制,异常处理等。所以这时我们就需要定义一个自己的拦截器了!

拦截器配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<!-- 拦截器默认包 -->
	<package name="interceptor-default" namespace="/" extends="struts-default">
		<!-- 定义一个拦截器 -->
		<interceptors>
			<!-- 自定义拦截器  -->
			<interceptor name="authority"
				class="com.xxxinterceptot.AuthorityInterceptor">
			</interceptor>
			<!-- 拦截器栈 -->
			<interceptor-stack name="authorityInterceptor">
				<interceptor-ref name="defaultStack" />
				<interceptor-ref name="authority" />
			</interceptor-stack>
		</interceptors>
		<!-- 设置成为默认拦截器 -->
		<default-interceptor-ref name="authorityInterceptor" />
		<!--  定义全局跳转页面 -->
		<global-results>
			<!-- 拦截器异常 -->
			<result name="interceptotException">error.jsp</result>
			<!-- 服务层异常 -->
			<result name="serviceException">error.jsp</result>
			<!-- 登录异常 -->
			<result name="loginException">login_error.jsp</result>
			<!-- 登录页面 -->
			<result name="login">timeout_error.jsp</result>
			<!-- 错误页面 -->
			<result name="error">error.jsp</result>
		</global-results>
		<!-- 定义异常捕获页面 -->
		<global-exception-mappings>
			<exception-mapping exception="com.xxx.exception.ServiceException" result="serviceException"/>
			<exception-mapping exception="com.xxx.exception.LoginException" result="loginException"/>
			<exception-mapping exception="com.xxx.exception.InterceptotException" result="interceptotException"/>
		</global-exception-mappings>
	</package>
</struts>    

 拦截器实现:

public class AuthorityInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// 获取request请求
	           HttpServletRequest request = ServletActionContext.getRequest();
		// 获取response请求
		HttpServletResponse response = ServletActionContext.getResponse();
		// 跳出拦截器,进入action
                return invocation.invoke();

                 }
}

    

你可能感兴趣的:(apache,jsp,xml,struts)