首页 > 代码库 > SpringMvc 拦截器

SpringMvc 拦截器

 

SpringMvc拦截器实现步骤

   1.编写拦截器实现HandlerInterceptor接口

   2.将拦截器注册进SpringMvc框架

   3.配置拦截规则

 

<mvc:interceptors> <mvc:interceptor>   <mvc:mapping path=”拦截的路径,可用正则表达式”/><bean class=”自定义拦截器实现类”></bean></mvc:interceptor></mvc:interceptors>

 使用springMvc自带拦截器解决post乱码

<!-- 解决spring post 中文乱码 --><filter>          <filter-name>characterEncodingFilter</filter-name>          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>          <init-param>              <param-name>encoding</param-name>              <param-value>UTF-8</param-value>          </init-param>          <init-param>              <param-name>forceEncoding</param-name>              <param-value>true</param-value>          </init-param>      </filter>      <filter-mapping>          <filter-name>characterEncodingFilter</filter-name>          <url-pattern>/*</url-pattern>      </filter-mapping>  

用户权限判断

@Override    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,            Object arg2) throws Exception {        // TODO Auto-generated method stub        if(arg0.getSession().getAttribute("user")==null){            arg0.getRequestDispatcher("/login.jsp").forward(arg0, arg1);            return false;        }        return true;    }

 

SpringMvc 拦截器