spring设置代码实例
(拦截都是控制层的地址。 filter: )
创建⼀个类实现接⼝ HandlerInterceptor
package com.zhiyou100.wyf.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("结束");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("预备");
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {    Object name = Session().getAttribute("name");
if(name!=null) {
return true;
}else {
response.sendRedirect("../denglu.jsp");
return false;
}
}
}
重写接⼝的中⽅法
把创建的类配置到springmvc⽂件中。
<!-- 配置 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/**"/>mvc实例
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/register"/>
<mvc:exclude-mapping path="/user/stu1"/>
<bean class="com.zhiyou100.wyf.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。