SpringBoot - Data validation of forms

Keywords: Spring SpringBoot Hibernate

SpringBoot uses the Hibernate-validate checking framework

ValidateRules

  1. @ NotBlank: Determines whether a string is null or empty (removing the first and last spaces).
  2. @ NotEmpty: Determines whether a string is null or empty.
  3. @ Length: Determine the length of a character (maximum or minimum)
  4. @ Min: Judging the Minimum Value
  5. @ Max: Judging the Maximum Value
  6. @ Email: Judging the Legality of Mailboxes

SpringBook form data validation steps

1. Adding validation rules to entity classes

public class Users {
    @NotBlank(message = "User name cannot be empty")
    private String name;
    @Min(12)
    @NotNull
    private Integer age;
    private String password;

    /*getter and setter*/
}

2. Open school in Controller

package com.zth.controller;

import com.zth.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

/**
 * @author zth
 * @Date 2019-07-25 11:25
 */
@Controller
public class UsersController {
    /**
     * Page Jump
     */
    @RequestMapping("/addUser")
    public String showPage(){
        return "add";
    }
    
    /**
     * Adding users
     * @Valid Open data validation for Users objects
     * @param result The results of the verification are encapsulated.
     */
    @RequestMapping("/save")
    public String addUser(@Valid Users users, BindingResult result){
        if (result.hasErrors()){
            return "add";
        }
        System.out.println(users);
        return "ok";
    }
}

3. Get prompt information on the page

add.html: Submit the Add User Form

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="UTF-8">
	<title>Adding users</title>
</head>
<body>
<center>
	<form th:action="@{/save}" method="post">
		User Name:<input type="text" name="name"/>
        <font color="red" th:errors="${users.name}"></font><br/>
        User Age:<input type="text" name="age" />
        <font color="red" th:errors="${users.age}"></font><br/>
		User password:<input type="password" name="password" /><br>
        <input type="submit" value="OK"/>
	</form>
</center>
</body>
</html>

ok.html: Operational Success Page

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Operational Tips Page</title>
</head>
<body>
	//Successful operation!!!
</body>
</html>

4. Encountering anomalies

5. Solutions

An object is injected into the method of jumping pages to solve the problem. The variable name of the parameter object must be lowercase for the full initials of the class name of the object.

 /**
     * Page Jump
     * Because spring MVC passes this object into Model.
     * key The name of the object uses the hump-like naming rule as the key.
     * The variable name of the parameter needs to be the same as that of the object. Lowercase the initials
     */
    @RequestMapping("/addUser")
    public String showPage(Users user){
        return "add";
    }

6. Operation effect:

 

7. Changing the name of the parameter

Use @ModelAttribute("aa")

package com.zth.controller;

import com.zth.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

/**
 * @author zth
 * @Date 2019-07-25 11:25
 */
@Controller
public class UsersController {
    /**
     * Page Jump
     * Because spring MVC passes this object into Model.
     * key The name of the object uses the hump-like naming rule as the key.
     * The variable name of the parameter needs to be the same as that of the object. Lowercase the initials
     *
     * If you want to change the name of the object being passed, you can use @ModelAttribute("aa") to indicate when
     * The key of the object passed before is aa.
     */
    @RequestMapping("/addUser")
    public String showPage(@ModelAttribute("aa") Users user){
        return "add";
    }

    /**
     * Adding users
     * @Valid Open data validation for Users objects
     * @param result The results of the verification are encapsulated.
     */
    @RequestMapping("/save")
    public String addUser(@ModelAttribute("aa") @Valid Users users, BindingResult result){
        if (result.hasErrors()){
            return "add";
        }
        System.out.println(users);
        return "ok";
    }
}
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="UTF-8">
	<title>Adding users</title>
</head>
<body>
<center>
	<form th:action="@{/save}" method="post">
		User Name:<input type="text" name="name"/>
        <font color="red" th:errors="${aa.name}"></font><br/>
        User Age:<input type="text" name="age" />
        <font color="red" th:errors="${aa.age}"></font><br/>
		User password:<input type="password" name="password" /><br>
        <input type="submit" value="OK"/>
	</form>
</center>
</body>
</html>

 

 

Posted by shush on Tue, 30 Jul 2019 12:12:25 -0700