SpringBoot服务端表单数据校验

Posted Biexiansheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot服务端表单数据校验相关的知识,希望对你有一定的参考价值。

1、首先说明一下,这里使用的是Springboot2.2.6.RELEASE版本,由于Springboot迭代很快,所以要注意版本问题。

  SpringBoot对表单做数据校验,SpringBoot对表单数据校验的技术特点,SpringBoot中使用了Hibernate-validate校验框架(SpringBoot的Web启动器中已经包含了Hibernate-validate校验框架的依赖jar包)。

2、SpringBoot表单数据校验步骤。

2.1、第一步,在实体类中添加校验规则,在被校验的实体类中要被校验的属性,使用注解进行校验。

 1 package com.bie.springboothello.po;
 2 
 3 import javax.validation.constraints.NotBlank;
 4 
 5 public class Users 
 6 
 7     private Integer id;//用户编号
 8     @NotBlank // 非空检验,表示当前属性不能为空。
 9     private String name;//用户账号
10     @NotBlank //密码非空校验
11     private Integer age;//用户密码
12 
13     public Integer getId() 
14         return id;
15     
16 
17     public void setId(Integer id) 
18         this.id = id;
19     
20 
21     public String getName() 
22         return name;
23     
24 
25     public void setName(String name) 
26         this.name = name;
27     
28 
29     public Integer getAge() 
30         return age;
31     
32 
33     public void setAge(Integer age) 
34         this.age = age;
35     
36 
37     @Override
38     public String toString() 
39         return "Users" +
40                 "id=" + id +
41                 ", name='" + name + '\\'' +
42                 ", age=" + age +
43                 '';
44     
45 
46     public Users(Integer id, String name, Integer age) 
47         this.id = id;
48         this.name = name;
49         this.age = age;
50     
51 

2.2、第二步,在Controller中开启校验。

 1 package com.bie.springboothello.controller;
 2 
 3 import com.bie.springboothello.po.Users;
 4 import com.bie.springboothello.service.UsersService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.validation.BindingResult;
 9 import org.springframework.web.bind.annotation.PathVariable;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 
13 import javax.validation.Valid;
14 import java.util.List;
15 
16 /**
17  * SpringBoot表单数据校验
18  */
19 
20 @Controller
21 @RequestMapping(value = "/users")
22 public class UsersController 
23 
24     @Autowired
25     private UsersService usersService;
26 
27 
28     /**
29      * 页面跳转
30      * <p>
31      * <p>
32      * thymeleaf框架,模板级的视图层技术,需要放到classpath的根目录下,也就是src/main/resources下面的一个包叫做templates的目录下面
33      *
34      * <p>
35      * <p>
36      * 由于templates目录是安全的,不允许直接被外界访问的,所以将视图放到这里面之后,必须有Controller里面的方法
37      * 帮助做页面的跳转,此方法的作用就是做页面的跳转。
38      *
39      * @param page
40      * @return
41      */
42     @RequestMapping(value = "/page")
43     public String showPage(@PathVariable String page) 
44         System.out.println("============================================" + page);
45         return page;
46     
47 
48 
49     /**
50      * 添加用户信息
51      *
52      * @param users         @Valid注解表示开启对Users对象的数据校验。
53      * @param bindingResult 校验过程中产生的校验结果,校验结果存放到BindingResult中。封装了校验的结果。
54      *                      对校验结果的成功或者错误的提示信息进行回显进行提示。
55      * @return
56      */
57     @RequestMapping(value = "addUser")
58     public String addUser(@Valid Users users, BindingResult bindingResult) 
59         // 如果校验失败了,从那个页面来返回到那个界面即可,并进行提示。
60         if (bindingResult.hasErrors()) 
61             return "input";
62         
63         this.usersService.addUser(users);
64         return "redirect:/users/findUserAll";
65     
66 
67 
68 

2.3、第三步,在页面中获取提示信息。

 1 <!DOCTYPE html>
 2 <html xmlns:th="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>添加用户</title>
 6 </head>
 7 <body>
 8 <h1 style="text-align: center">用户管理~新增用户</h1>
 9 <hr/>
10 
11 <div style="text-align: center">
12     <form th:action="@/users/addUser" method="post">
13 
14         用户姓名:<input type="text" name="name"/>
15         <!-- 根据users.name去BindingResult对象里面查找的key,然后找到错误信息进行提示,按照参数对象的驼峰方式进行命名,这个驼峰命名的就是前端要使用的对象 -->
16         <span color="red" th:errors="$users.name"></span><br/>
17         用户年龄:<input type="text" name="age"/>
18         <span color="red" th:errors="$users.age"></span><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
19         <input type="submit" value="确定"/> &nbsp;&nbsp;&nbsp;
20         <input type="reset" value="重置"/><br/>
21     </form>
22 </div>
23 
24 </body>
25 </html>

嗯,报错了,说找不到users属性了。因为这里面的跳转是,如果验证出现了问题,那么跳转到输入界面,而这个跳转到输入界面是由Controller的一个方法跳转的,此时这个方法没有users属性,导致的报错。

  1 2020-05-11 22:55:18.726 ERROR 5052 --- [0.1-8080-exec-2] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-127.0.0.1-8080-exec-2] Exception processing template "input": Error during execution of processor 'org.thymeleaf.spring5.processor.SpringErrorsTagProcessor' (template: "input" - line 16, col 27)
  2 
  3 org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringErrorsTagProcessor' (template: "input" - line 16, col 27)
  4     at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
  5     at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
  6     at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
  7     at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
  8     at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
  9     at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 10     at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:592) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 11     at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 12     at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 13     at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) [thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 14     at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) [thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 15     at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 16     at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 17     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 18     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 19     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 20     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 21     at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) [tomcat-embed-core-9.0.33.jar:9.0.33]
 22     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 23     at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) [tomcat-embed-core-9.0.33.jar:9.0.33]
 24     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-9.0.33.jar:9.0.33]
 25     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
 26     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) [tomcat-embed-websocket-9.0.33.jar:9.0.33]
 27     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
 28     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
 29     at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 30     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 31     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
 32     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
 33     at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 34     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 35     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
 36     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
 37     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 38     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 39     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.33.jar:9.0.33]
 40     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.33.jar:9.0.33]
 41     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.33.jar:9.0.33]
 42     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.33.jar:9.0.33]
 43     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.33.jar:9.0.33]
 44     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.33.jar:9.0.33]
 45     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.33.jar:9.0.33]
 46     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.33.jar:9.0.33]
 47     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.33.jar:9.0.33]
 48     at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.33.jar:9.0.33]
 49     at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.33.jar:9.0.33]
 50     at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.33.jar:9.0.33]
 51     at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) [tomcat-embed-core-9.0.33.jar:9.0.33]
 52     at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.33.jar:9.0.33]
 53     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_191]
 54     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_191]
 55     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.33.jar:9.0.33]
 56     at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]
 57 Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'users' available as request attribute
 58     at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 59     at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 60     at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 61     at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 62     at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:258) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 63     at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 64     at org.thymeleaf.spring5.processor.SpringErrorsTagProcessor.doProcess(SpringErrorsTagProcessor.java:65) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 65     at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 66     ... 52 common frames omitted
 67 
 68 2020-05-11 22:55:18.729 ERROR 5052 --- [0.1-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringErrorsTagProcessor' (template: "input" - line 16, col 27)] with root cause
 69 
 70 java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'users' available as request attribute
 71     at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 72     at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 73     at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 74     at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 75     at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:258) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 76     at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 77     at org.thymeleaf.spring5.processor.SpringErrorsTagProcessor.doProcess(SpringErrorsTagProcessor.java:65) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 78     at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 79     at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 80     at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 81     at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 82     at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 83     at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 84     at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:592) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 85     at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 86     at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 87     at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 88     at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
 89     at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 90     at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 91     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 92     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 93     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 94     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 95     at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
 96     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
 97     at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
 98     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
 99     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
100     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.33.jar:9.0.33]
101     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
102     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
103     at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
104     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
105     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
106     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
107     at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
108     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
109     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
110     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
111     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
112     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
113     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
114     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
115     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
116     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.33.jar:9.0.33]
117     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.33.jar:9.0.33]
118     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.33.jar:9.0.33]
119     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.33.jar:9.0.33]
120     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.33.jar:9.0.33]
121     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.33.jar:9.0.33]
122     at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.33.jar:9.0.33]
123     at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.33.jar:9.0.33]
124     at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.33.jar:9.0.33]
125     at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) [tomcat-embed-core-9.0.33.jar:9.0.33]
126     at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.33.jar:9.0.33]
127     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_191]
128     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_191]
129     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.33.jar:9.0.33]
130     at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]

解决数据校验时的异常问题,解决异常的方法,在跳转页面的方法中注入一个对象,来解决问题。要求参数对象的,变量名必须是对象的类名的全称首字母小写。

 1 package com.bie.springboothello.controller;
 2 
 3 import com.bie.springboothello.po.Users;
 4 import com.bie.springboothello.service.UsersService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.validation.BindingResult;
 9 import org.springframework.web.bind.annotation.PathVariable;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 
13 import javax.validation.Valid;
14 import java.util.List;
15 
16 /**
17  * SpringBoot表单数据校验
18  */
19 
20 @Controller
21 @RequestMapping(value = "/users")
22 public class UsersController 
23 
24     @Autowired
25     private UsersService usersService;
26 
27     /**
28      * 页面跳转
29      * <p>
30      * <p>
31      * thymeleaf框架,模板级的视图层技术,需要放到classpath的根目录下,也就是src/main/resources下面的一个包叫做templates的目录下面
32      *
33      * <p>
34      * <p>
35      * 由于templates目录是安全的,不允许直接被外界访问的,所以将视图放到这里面之后,必须有Controller里面的方法
36      * 帮助做页面的跳转,此方法的作用就是做页面的跳转。
37      * <p>
38      * <p>
39      * 解决异常的方式。可以在跳转页面的方法中注入一个 Uesrs 对象。
40      * 注意:由于springmvc会将该对象放入到Model中传递。key的名称会使用该对象的驼峰式的命名规则来作为key。
41      * 参数的变量名需要与对象的名称相同。将首字母小写。
42      *
43      * @param page
44      * @return
45      */
46     @RequestMapping(value = "/page")
47     public String showPage(@PathVariable String page, Users users) 
48         System.out.println("============================================" + page);
49         return page;
50     
51 
52 
53     /**
54      * 添加用户信息
55      *
56      * @param users         @Valid注解表示开启对Users对象的数据校验。
57      * @param bindingResult 校验过程中产生的校验结果,校验结果存放到BindingResult中。封装了校验的结果。
58      *                      对校验结果的成功或者错误的提示信息进行回显进行提示。
59      * @return
60      */
61     @RequestMapping(value = "addUser")
62     public String addUser(@Valid Users users, BindingResult bindingResult) 
63         // 如果校验失败了,从那个页面来返回到那个界面即可,并进行提示。
64         if (bindingResult.hasErrors()) 
65             return "input";
66         
67         this.usersService.addUser(users);
68         return "redirect:/users/findUserAll";
69     
70 
71 

嗯,又报错了。这里需要注意的是,我的年龄age写成的是Integer类型的。Java实体类中属性是Integer类型,用了NotBlank判断不能为空,而这个注解是判断字符串String是否为空。此时可以使用使用@NotNull, 去掉@NotBlank注解。

备注:使用hibernate validator需要注意@NotNull和@NotEmpty和@NotBlank区别。

a、@NotEmpty用在集合类上面。
b、@NotBlank用在String上面。
c、@NotNull用在基本类型上。

如果在基本类型上面用NotEmpty或者NotBlank 会出现上面的错。

 1 2020-05-14 10:20:19.944 ERROR 1924 --- [0.1-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.Integer'. Check configuration for 'age'] with root cause
 2 
 3 javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.Integer'. Check configuration for 'age'
 4     at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getExceptionForNullValidator(ConstraintTree.java:108) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
 5     at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:140) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
 6     at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:55) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
 7     at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:73) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
 8     at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:127) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
 9     at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:120) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
10     at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:552) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
11     at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:515) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
12     at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:485) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
13     at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:447) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
14     at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:397) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
15     at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:173) ~[hibernate-validator-6.0.18.Final.jar:6.0.18.Final]
16     at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:109) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
17     at org.springframework.boot.autoconfigure.validation.ValidatorAdapter.validate(ValidatorAdapter.java:65) ~[spring-boot-autoconfigure-2.2.6.RELEASE.jar:2.2.6.RELEASE]
18     at org.springframework.validation.DataBinder.validate(DataBinder.java:892) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
19     at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.validateIfApplicable(ModelAttributeMethodProcessor.java:360) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
20     at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:162) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
21     at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
22     at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
23     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
24     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
25     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
26     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
27     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
28     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
29     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
30     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
31     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
32     at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
33     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.5.RELEASE.jar:5.2.5.RELEASE]
34     at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
35     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
36     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
37     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.33.jar:9.0.33]
38     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
39     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
40     at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
41     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
42     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
43     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
44     at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
45     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
46     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
47     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
48     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
49     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
50     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
51     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
52     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
53     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.33.jar:9.0.33]
54     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) [tomcat-embed-core-9.0.33.jar:9.0.33]
55     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.33.jar:9.0.33]
56     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.33.jar:9.0.33]
57     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.33.jar:9.0.33]
58     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.33.jar:9.0.33]
59     at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) [tomcat-embed-core-9.0.33.jar:9.0.33]
60     at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.33.jar:9.0.33]
61     at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-9.0.33.jar:9.0.33]
62     at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594) [tomcat-embed-core-9.0.33.jar:9.0.33]
63     at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.33.jar:9.0.33]
64     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_191]
65     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_191]
66     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.33.jar:9.0.33]
67     at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]

如果参数的名称需要做改变。如果想为传递的对象更改名称,可以使用@ModelAttribute("user")这表示当 前传递的对象的 key 为 user。那么我们在页面中获取该对象的 key 也需要修改为 user。

 1 package com.bie.springboothello.controller;
 2 
 3 import com.bie.springboothello.po.Users;
 4 import com.bie.springboothello.service.UsersService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.ui.Model;
 8 import org.springframework.validation.BindingResult;
 9 import org.springframework.web.bind.annotation.ModelAttribute;
10 import org.springframework.web.bind.annotation.PathVariable;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.ResponseBody;
13 
14 import javax.validation.Valid;
15 import java.util.List;
16 
17 /**
18  * SpringBoot表单数据校验
19  */
20 
21 @Controller
22 @RequestMapping(value = "/users")
23 public class UsersController 
24 
25     @Autowired
26     private UsersService usersService;
27     
28     /**
29      * 页面跳转
30      * <p>
31      * <p>
32      * thymeleaf框架,模板级的视图层技术,需要放到classpath的根目录下,也就是src/main/resources下面的一个包叫做templates的目录下面
33      *
34      * <p>
35      * <p>
36      * 由于templates目录是安全的,不允许直接被外界访问的,所以将视图放到这里面之后,必须有Controller里面的方法
37      * 帮助做页面的跳转,此方法的作用就是做页面的跳转。
38      * <p>
39      * <p>
40      * 解决异常的方式。可以在跳转页面的方法中注入一个 Uesrs 对象。
41      * 注意:由于springmvc会将该对象放入到Model中传递。key的名称会使用该对象的驼峰式的命名规则来作为key。
42      * 参数的变量名需要与对象的名称相同。将首字母小写。
43      * <p>
44      * <p>
45      * 如果想为传递的对象更改名称,可以使用@ModelAttribute("user")这表示当 前传递的对象的 key 为 user。
46      * 那么我们在页面中获取该对象的 key 也需要修改为 user
47      *
48      * @param page
49      * @return
50      */
51     @RequestMapping(value = "/page")
52     public String showPage(@PathVariable String page, @ModelAttribute(value = "user") Users users) 
53         System.out.println("============================================" + page);
54         return page;
55     
56 
57 
58     /**
59      * 添加用户信息
60      *
61      * @param users         @Valid注解表示开启对Users对象的数据校验。
62      * @param bindingResult 校验过程中产生的校验结果,校验结果存放到BindingResult中。封装了校验的结果。
63      *                      对校验结果的成功或者错误的提示信息进行回显进行提示。
64      * @return
65      */
66     @RequestMapping(value = "addUser")
67     public String addUser(@ModelAttribute(value = "user") @Valid Users users, BindingResult bindingResult) 
68         // 如果校验失败了,从那个页面来返回到那个界面即可,并进行提示。
69         if (bindingResult.hasErrors()) 
70             return "input";
71         
72         this.usersService.addUser(users);
73         return "redirect:/users/findUserAll";
74     
75 
76 
 1 <!DOCTYPE html>
 2 <html xmlns:th="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>添加用户</title>
 6 </head>
 7 <body>
 8 <h1 style="text-align: center">用户管理~新增用户</h1>
 9 <hr/>
10 
11 <div style="text-align: center">
12     <form th:action="@/users/addUser" method="post">
13 
14         用户姓名:<input type="text" name="name"/>
15         <!-- 根据users.name去BindingResult对象里面查找的key,然后找到错误信息进行提示 -->
16         <span style="color:red" th:errors="$user.name"></span><br/>
17         用户年龄:<input type="text" name="age"/>
18         <span style="color:red" th:errors="$user.age"></span><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
19         <input type="submit" value="确定"/> &nbsp;&nbsp;&nbsp;
20         <input type="reset" value="重置"/><br/>
21     </form>
22 </div>
23 
24 </body>
25 </html>

演示效果,如下所示:

如何指定验证提示信息呢。

 1 package com.bie.springboothello.po;
 2 
 3 import javax.validation.constraints.NotBlank;
 4 import javax.validation.constraints.NotNull;
 5 
 6 public class Users 
 7 
 8     private Integer id;//用户编号
 9     @NotBlank(message = "用户账号不能为空.") // 用户账号,非空检验
10     private String name;//用户账号
11     @NotNull(message = "用户密码不能为空") //用户密码,非空校验
12     private Integer age;//用户密码
13 
14     public Integer getId() 
15         return id;
16     
17 
18     public void setId(Integer id) 
19         this.id = id;
20     
21 
22     public String getName() 
23         return name;
24     
25 
26     public void setName(String name) 
27         this.name = name;
28     
29 
30     public Integer getAge() 
31         return age;
32     
33 
34     public void setAge(Integer age) 
35         this.age = age;
36     
37 
38     @Override
39     public String toString() 
40         return "Users" +
41                 "id=" + id +
42                 ", name='" + name + '\\'' +
43                 ", age=" + age +
44                 '';
45     
46 
47     public Users(Integer id, String name, Integer age) 
48         this.id = id;
49         this.name = name;
50         this.age = age;
51     
52 

演示效果,如下所示:

使用hibernate validator其他校验规则。

1)、@NotBlank: 判断字符串是否为null或者是空串(去掉首尾空格)。
2)、@NotEmpty: 判断字符串是否null或者是空串。
3)、@Length: 判断字符的长度(最大或者最小)。  
4)、@Min: 判断数值最小值。
5)、@Max: 判断数值最大值。
6)、@Email: 判断邮箱是否合法。
7)、@NotNull用在基本类型上。

 

以上是关于SpringBoot服务端表单数据校验的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot之服务端数据校验

使用篇SpringBoot服务端数据校验

Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)七(品牌查询,品牌提交表单信息以及表单页面信息和校验)以及axios(Ajax)的使用(代码

表单校验

vue 表单校验

SpringMVC 数据校验(JSR-303)