Add Form Verification hibernate-validator in spring boot and display the error message in the freemarker template (recommended ),

Source: Internet
Author: User

Add Form Verification hibernate-validator in spring boot and display the error message in the freemarker template (recommended ),

Create a project

Use IDEA to create a spring-boot project. Select web, validation, and freemarker for dependencies.

First look at the effect

 

Create object class

Create and add annotations. The Code is as follows:

Public class Person implements Serializable {@ NotNull @ Length (min = 3, max = 10) // The Length of username is between 3 and 10 private String username; @ NotNull @ Min (18) // The minimum age is 18 years old. private Integer age; @ NotNull // use the regular expression to verify the field. message: set the verification Failure Information @ Pattern (regexp = "[\ w-\.] + @ ([\ w-] + \\.) + [a-z] {2, 3} ", message =" Incorrect email format ") private String email; public String getEmail () {return email ;} public void setEmail (String email) {this. email = email;} public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public Integer getAge () {return age;} public void setAge (Integer age) {this. age = age ;}}

Configure controller

Code:

@ Controllerpublic class WebController extends WebMvcConfigurerAdapter {@ Override public void addViewControllers (ViewControllerRegistry registry) {// Add a route and set the Page name registry. addViewController ("/results "). setViewName ("results") ;}@ GetMapping ("/") public String showForm (Person person) {return "form" ;}@ PostMapping ("/") public String checkPersonInfo (@ Valid Person person, BindingResult bindingResult, RedirectAttributes redirectAttributes) {// use BindingResult to verify the correctness of form data if (bindingResult. hasErrors () {// returns the submitted Form content to the page and displays redirectAttributes. addFlashAttribute ("person", person); return "form" ;}return "redirect:/results ";}}

Note: Do not forget the @ Valid annotation

Form page

The form page uses the spring label to retrieve data that fails verification. To use the spring label in spring-boot, you can use spring. the ftl file is placed in resources, and then in application. add the following configuration to yml:

Spring. ftl file path: org.springframework.web.servlet.view.freemarker.spring.ftl

spring: freemarker:  settings:   auto_import: /spring.ftl as spring

Form Page code

<form action="/" method="post"> <div class="form-group">  <label for="username">username</label>  <@spring.bind "person.username"/>  <input type="text" id="username" name="username" value="${person.username!}" class="form-control"      placeholder="username"/>  <span class="text-danger"><@spring.showErrors ""/></span> </div> <div class="form-group">  <label for="age">age</label>  <@spring.bind "person.age"/>  <input type="number" id="age" name="age" value="${person.age!}" class="form-control" placeholder="age"/>  <span class="text-danger"><@spring.showErrors ""/></span> </div> <div class="form-group">  <label for="email">email</label>  <@spring.bind "person.email"/>  <input type="text" id="email" name="email" value="${person.email!}" class="form-control"      placeholder="email"/>  <span class="text-danger"><@spring.showErrors ""/></span> </div> <input type="submit" value="submit" class="btn btn-sm btn-primary"/></form>

Note: you must first use<@spring.bind "person.username"/>Bind the field, and then use<@spring.showErrors ""/> To retrieve the error message.

Reference

Https://spring.io/guides/gs/validating-form-input/

Summary

The above section describes how to add Form Verification hibernate-validator in spring boot and display the error message (recommended) in the freemarker template, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.