配置简单的拦截器java中

springMVC.xml文件中====

<!-- 拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/*"/>
<mvc:exclude-mapping path="/**/fonts/*"/>
<mvc:exclude-mapping path="/**/*.css"/>
<mvc:exclude-mapping path="/**/*.js"/>
<mvc:exclude-mapping path="/**/*.png"/>
<mvc:exclude-mapping path="/**/*.gif"/>
<mvc:exclude-mapping path="/**/*.jpg"/>
<mvc:exclude-mapping path="/**/*.jpeg"/>
<!-- 登录前放行的请求 -->
<mvc:exclude-mapping path="/**/user/login.do"/>
<mvc:exclude-mapping path="/**/user/toRegister.do"/>
<mvc:exclude-mapping path="/**/code/getVerifyCode"/>
<mvc:exclude-mapping path="/**/user/findUserByPhoneNo.do"/>
<mvc:exclude-mapping path="/**/user/findAllotControlByUserNo.do"/>
<mvc:exclude-mapping path="/**/user/userLogin.do"/>
<mvc:exclude-mapping path="/**/user/toUserMainjsp.do"/>
<bean class="interceptor.InterceptorDemo"></bean>
</mvc:interceptor>
</mvc:interceptors>

java代码

package interceptor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InterceptorDemo implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("进入preHandle方法");
String user_no=httpServletRequest.getSession().getAttribute("user_no").toString();
System.out.println("进入preHandle方法user_no=="+user_no);
if(!StringUtils.isAllBlank(user_no)) {
System.out.println("session有值");
return true;
}
return false;
}

@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
String url = httpServletRequest.getServletPath();//获取当前url
String contextPath = httpServletRequest.getContextPath();//获取当前项目的根目录
System.out.println("url:"+url);
System.out.println("contextPath:"+contextPath);
System.out.println("进入postHandler 方法");
}

@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("进入afterCompletion 方法");

}
}

上一篇:RedisSession (自定义)


下一篇:Spring注入的成员变量HttpServletRequest是线程安全的吗?【享学Spring MVC】