• 每天学点SpringMVC-数据校验、JSON格式返回数据、文件上传


    1. Springmvc中 JSR 303验证标准的实现

        1.1 添加maven相关依赖

    <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
          <groupId>javax.validation</groupId>
          <artifactId>validation-api</artifactId>
          <version>2.0.0.Final</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
        <dependency>
          <groupId>org.hibernate.validator</groupId>
          <artifactId>hibernate-validator</artifactId>
          <version>6.0.2.Final</version>
        </dependency>

       1.2 spring-mvc.xml配置中添加<mvc:annoation-driven/>标签

       1.3 对应的属性上添加注解,如下,需要lastName字段不能为空

        

        1.4 目标方法入参中对应的参数添加@Valid注解

       

       1.5 方法入参中添加BindingResult,用于保存校验失败的信息

      1.6 使用<form:errors>标签用于回显错误信息

      1.7 代码运行结果如下

      1.8 有个小坑:在方法入参Employee没有指定@ModelAttribute注解并指定value时,会以employee作为key去保存POJO对象以及errors相关信息,而在input页面的表单在没有指定modelattribute值时,默认以key=command读取POJO、Errors等相关信息,所以在Employee参数上添加@ModelAttribute注解并指定value=command。

     2. 使用Srpingmvc返回JSON数据

         2.1 添加maven依赖

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.1</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.9.1</version>
        </dependency>
    
       <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.9.1</version>
        </dependency>

      2.2 在目标方法上添加@ResponseBody注解并以POJO对象或其集合的形式返回

      2.3 结果如下:

      用视频教程老师的话:简单到没有朋友

    3. Easy to Upload File With SpringMVC

      3.1 添加Maven依赖

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.5</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.3</version>
        </dependency>

          spring-mvc.xml配置

     <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="utf-8"/>
            <property name="maxUploadSize" value="1024000"/>
        </bean>

       注:这里的id必须为  multipartResolver ,否则会一直失败

       3.2 书写Controller

    @ResponseBody
        @RequestMapping("/testFileUpload.do")
        public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file")MultipartFile file){
            return "文件上传成功" + ",文件名: " + file.getOriginalFilename() + ",文件描述:" + desc;
        }

      3.3 测试一下:

    <form action="test/testFileUpload.do" enctype="multipart/form-data" method="post">
    File : <input type="file" name="file">
    Desc : <input type="text" name="desc">
    <input type="submit">
    </form>
  • 相关阅读:
    【HTML】input标签中alt属性和title属性的比较
    【HTML】WWW简介
    【MySQL】MySQL的常规操作
    iOS编程(双语版)
    Swift语言精要
    Swift语言精要
    python网络爬虫
    Python小任务
    如何在onCreate方法中获取视图的宽度和高度
    python网络爬虫
  • 原文地址:https://www.cnblogs.com/xpawn/p/7588789.html
Copyright © 2020-2023  润新知