Spring boot server form data verification

  • POM dependence
<parent>
    <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.10.RELEASE</version>
</parent>

<properties>
	<java.version>1.8</java.version>
	<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
	<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot Starter -->
	<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
<!-- thymeleaf Starter -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
</dependencies>
  • Hibernate validate is used in SpringBoot
  1. Add validation rule in entity class
    @NotBlank: judge whether the string is null or empty (remove the first and last spaces).
    @NotEmpty: determines whether the string is null or empty.
    @Length: determines the length of the character (maximum or minimum)
    @Min: minimum value of judgment value
    @Max: maximum value of judgment value
    @Email: judge whether the email is legal
public class Users {
		@NotBlank //Non empty calibration
		private String name;
		@NotBlank //Password non empty verification
		private String password;
		private Integer age;
}
  1. Enable verification in Controller
/**
* Complete user add
*@Valid Enable data validation for Users object
*BindingResult:Encapsulates the verification results
*/
@RequestMapping("/save")
public String saveUser(@Valid Users users,BindingResult result){
	if(result.hasErrors()){
		return "add";
	}
	System.out.println(users);
	return "ok";
}
  1. Get tips on the page
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Add user</title>
	</head>
<body>
	<form th:action="@{/save}" method="post">
		User name:<input type="text" name="name"/><font color="red" th:errors="${users.name}"></font><br/>
		User password:<input type="password" name="password"/><font color="red" th:errors="${users.password}"></font><br/>
		User age:<input type="text" name="age" /><font color="red"th:errors="${users.age}"></font><br/>
		<input type="submit" value="OK"/>
	</form>
</body>
</html>
  • In case of exception, solve the problem of exception during data verification
  1. The solution to the exception is to inject an object into the jump page method to solve the problem. It is required that the variable name of the parameter object must be the full name of the class name of the object in lowercase.
/**
* How to resolve the exception. You can inject a Uesrs object into the method of jump page.
* Note: because spring MVC will pass the object into the Model. The name of the key will use
 The hump type naming rule of the object is used as the key.
* The variable name of the parameter needs to be the same as the name of the object. Lowercase the initial.
*
* @param users
* @return
*/
@RequestMapping("/addUser")
public String showPage( Users users){
		return "add";
}
/**
* Complete user add
*@Valid Enable data validation for Users object
*BindingResult:Encapsulates the verification results
*/
@RequestMapping("/save")
	public String saveUser( @Valid Users users,BindingResult result){
	if(result.hasErrors()){
		return "add";
	}
	System.out.println(users);
	return "ok";
}

//===============================================
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Add user</title>
	</head>
<body>
	<form th:action="@{/save}" method="post">
	User name:<input type="text" name="name"/><font color="red" th:errors="${users.name}"></font><br/>
	User password:<input type="password" name="password" /><font color="red" th:errors="${users.password}"></font><br/>
	User age:<input type="text" name="age" /><font color="red" th:errors="${users.age}"></font><br/>
	<input type="submit" value="OK"/>
	</form>
</body>
</html>

  1. If the parameter name needs to be changed
/**
*
* If you want to change the name of the passed object, you can use @ ModelAttribute("aa") to indicate that when
 The key of the previously passed object is aa.
* Then we need to change the key of the object to aa
* @param users
* @return
*/
@RequestMapping("/addUser")
public String showPage(@ModelAttribute("aa") Users users){
		return "add";
}
/**
* User add complete
*@Valid Enable data validation for Users object
*BindingResult:Encapsulates the verification results
*/
@RequestMapping("/save")
public String saveUser(@ModelAttribute("aa") @Valid Users users,BindingResult result){
		if(result.hasErrors()){
		return "add";
		}
		System.out.println(users);
		return "ok";
}


========================================

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Add user</title>
	</head>
<body>
	<form th:action="@{/save}" method="post">
	User name:<input type="text" name="name"/><font color="red" th:errors="${aa.name}"></font><br/>
	User password:<input type="password" name="password"/><font color="red" th:errors="${aa.password}"></font><br/>
	User age:<input type="text" name="age" /><font color="red" th:errors="${aa.age}"></font><br/>
	<input type="submit" value="OK"/>
	</form>
</body>
</html>

Tags: Thymeleaf Spring Java SpringBoot

Posted on Tue, 03 Dec 2019 12:58:26 -0800 by stickynote427