Create a project
Use IDEA to create a spring-boot project, and select web, validation, and freemarker.
Check out the effect first
Create entity 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-10 private String username; @NotNull @Min(18) // The youngest is 18 years old private Integer age; @NotNull // Use regularity to verify the field, message: Set the information of verification failure @Pattern(regexp = "[//w-//.]+@([//w-]+//.)+[az]{2,3}", message = "The mailbox format is incorrect") 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()) { // Return the submitted form content to the page intact and display it redirectAttributes.addFlashAttribute("person", person); return "form"; } return "redirect:/results"; }}Note: Don't forget the @Valid annotation
Form page
The spring tag is used in the form page to get the data that failed to verify. If you want to use the spring tag in spring-boot, you can place the spring.ftl file in resources, and then add the following configuration in application.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> <label for="username">username</label> <@spring.bind "person.username"/> <input type="text" id="username" name="username" value="${person.username!}" placeholder="username"/> <span><@spring.showErrors ""//</span> </div> <div> <label for="age">age</label> <@spring.bind "person.age"/> <input type="number" id="age" name="age" value="${person.age!}" placeholder="age"/> <span><@spring.showErrors ""/></span> </div> <div> <label for="email">email</label> <@spring.bind "person.email"/> <input type="text" id="email" name="email" value="${person.email!}" placeholder="email"/> <span><@spring.showErrors ""/></span> </div> <input type="submit" value="submit"/></form> Note: You must first use <@spring.bind "person.username"/> to bind the field, and then use <@spring.showErrors ""/> to get the error message
refer to
https://spring.io/guides/gs/validating-form-input/
Summarize
The above is the editor introduced to you that the form verification hibernate-validator is added to spring boot and displays error messages in the freemarker template (recommended). I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!