SpringMVC-三

文件上传

导入jar包

commons-fileupload-1.3.1.jar
commons-io-2.2.jar

前端提交

 

 springmvc配置文件中配置

 

 控制层代码

 

 

拦截器(拦截都是控制层的地址。在过滤器之后,可以共存 filter: )

1. 创建一个类 实现接口 HandlerInterceptor

2. 重写接口的中方法

public class MyInterceptor implements HandlerInterceptor{

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        System.out.println("aaaaaaaaaaaaaaaaa");
        HttpSession session=request.getSession();
        Object obj=session.getAttribute("user");
        System.out.println(obj);
        if(obj==null) {
            request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
            return false;
        }
        return true;
    }

}

 

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

 

 ##############################

 数据校验

 

 

 

 控制层用法

 

 遇到的错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/ValidationException

貌似是jdk版本较新

 

posted @ 2019-09-05 09:37  啊呜啊  阅读(117)  评论(0编辑  收藏  举报