亲宝软件园·资讯

展开

spring设置拦截器代码实例

人气:0

拦截器(拦截都是控制层的地址。 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 = request.getSession().getAttribute("name");
    if(name!=null) {
      return true;
    }else {
      response.sendRedirect("../denglu.jsp");
      return false;
    }
  }
}

重写接口的中方法

把创建的类配置到springmvc文件中。

<!-- 配置拦截器 -->
  <mvc:interceptors>
    <mvc:interceptor>
      <mvc:mapping path="/user/**"/>
      <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>
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论