JavaEE 之 后台验证+拦截器

1.Validator后台验证

  a.在web.xml中配置

      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/mySpring-*.xml
        </param-value>
    </context-param>
      <servlet>
        <servlet-name>myValidator</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
              /WEB-INF/mySpring-*.xml
          </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>myValidator</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

  b.在mySpring-servlet.xml中配置

    <!-- 启动注解驱动 -->
    <mvc:annotation-driven validator="validator"/>
    <!--启用自动扫描 -->
    <context:component-scan base-package="com.wode.controller" />

    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean ">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
    </bean>

  c.Controller中

    @RequestMapping(path="login",method=RequestMethod.POST)
    public ModelAndView login(@Valid UserBean user,BindingResult br){
        ModelAndView mod = new ModelAndView();
        if(br.hasErrors()){
            mod.setViewName("login");
            return mod;
        }else{
            mod.setViewName("main");
            mod.addObject("userName",user.getUserName());
            return mod;
        }
        
    }

  d.在Bean中

    @NotEmpty(message = "不能为空")
    @Length(max=16, min=8, message= "账户必须是8-16位")
    private String userName;
    
    @NotEmpty(message = "不能为空")
    private String userPwd;

  e.在html中

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags/form" %>
    <spring:form action="login.do" method="post" commandName="userBean">
        用户名:<input type="text" name="userName"><spring:errors delimiter="," path="userName"></spring:errors><br/>
         密码:<input type="password" name="userPwd"><spring:errors delimiter="," path="userPwd"></spring:errors><br/> 
         <input type="submit">
    </spring:form>

 

 

2.Interceptors拦截器

  a.在mySpring-servlet.xml中配置

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/*"/>
            <mvc:exclude-mapping path="/login.do"/>
            <bean class="com.wode.util.MyInterceptor" id="MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

  b.建拦截器类

    ①方法一:继承HandlerInterceptorAdapter类

public class MyInterceptor extends HandlerInterceptorAdapter{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // TODO Auto-generated method stub
        boolean flag = false;
        HttpSession session = request.getSession();
        if(session.getAttribute("user") != null){
            flag=true;
        }else{
            response.sendRedirect("login.jsp");
        }
        return flag;
    }        
}

    ②方法一:实现HandlerInterceptor接口

public class MyInterceptor2 implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("方法执行之后视图生成之前拦截");
        
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("方法执行之后拦截");
        
    }

    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("方法执行之前拦截");
        //ture--接着执行  false 停止执行
        //session 中如果没有用户信息 就重定向到登录页面
        return true;
    }

}

 

posted @ 2017-04-25 19:36  晨M风  阅读(1588)  评论(0编辑  收藏  举报