SpringMVC拦截器(过滤器):HandlerInterceptor接口,或者继承handlerinterceptoradapter
我这里使用的自定义拦截器,实现HandlerInterceptor
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String logname = (String) request.getSession().getAttribute("loginname");
if (logname != null) {
System.out.println("放行!");
return true;
} else {
System.out.println("拦截,跳转!");
response.sendRedirect("http://localhost:8080/cjw_salary_sheet/userInfo/error.do");
return false;
}
}
补全下面的 postHandle,afterCompletion
通过判断用户名是否存在来确定是否拦截
配置spring-mvc.xml`
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截所以请求-->
<mvc:mapping path="/**" />
<!-- 放行 .do /jsp / ?wsdl-->
<mvc:exclude-mapping path="/**/*?wsdl"/>
<mvc:exclude-mapping path="/userInfo/error.do" />
<mvc:exclude-mapping path="/userInfo/login.do"/>
<bean class="cn.apcinfo.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!--mvc:exclude-mapping 百度看一下放行规范, 可以放行当方法,静态资源(css.js...),以什么格式的(.jsp .html ....)-->