springboot actual notes (xii) ---- springboot common form data validation

Copyright: please indicate the source https://blog.csdn.net/qq_33223299 https://blog.csdn.net/qq_33223299/article/details/90674057

Register to create a simulation project

    pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
	</parent>
	<groupId>com.bjsxt</groupId>
	<artifactId>13-spring-boot-validate</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<java.version>1.7</java.version>
		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
	</properties>

	<dependencies>
		<!-- springBoot的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- thymeleaf的启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>
</project>

Create two entity classes

package com.bjsxt.pojo;

import javax.validation.constraints.Min;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;

public class Users {
	@NotBlank(message="用户名不能为空") //非空校验 判断字符串是否为 null 或者是空串(去掉首尾空格)。
	@Length(min=2,max=6,message="最小长度为2位,最大长度为6位")
	private String name;
	@NotEmpty//非空校验  判断字符串是否 null 或者是空串
	private String password;
	@Min(value=15)
	private Integer age;
	@Email(message="邮箱不合法")
	private String email;
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Users [name=" + name + ", password=" + password + ", age=" + age + "]";
	}
	
}

Three create Controller

/**
 * SpringBoot 表单数据校验
 *
 *
 */
@Controller
public class UsersController {
	/**
	 * 
	 * 如果想为传递的对象更改名称,可以使用@ModelAttribute("u")这表示当前传递的对象的key为aa。
	 * 那么我们在页面中获取该对象的key也需要修改为u
	 * @param users
	 * @return
	 */
	@RequestMapping("/addUser")
	public String showPage(@ModelAttribute("u") Users users){
		return "add";
	}
	
	/**
	 * 完成用户添加
	 *@Valid 开启对Users对象的数据校验
	 *BindingResult:封装了校验的结果
	 */
	@RequestMapping("/save")
	public String saveUser(@ModelAttribute("u") @Valid Users users,BindingResult result){
		if(result.hasErrors()){
			return "add";
		}
		System.out.println(users);
		return "success";
	}
}

 

 

Four create html

add.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
	<form th:action="@{/save}" method="post">
		用户姓名:<input type="text" name="name"/><font color="red" th:errors="${u.name}"></font><br/>
		用户密码:<input type="password" name="password" /><font color="red" th:errors="${u.password}"></font><br/>
		用户年龄:<input type="text" name="age" /><font color="red" th:errors="${u.age}"></font><br/>
		用户邮箱:<input type="text" name="email" /><font color="red" th:errors="${u.email}"></font><br/>
		<input type="submit" value="注册"/>
	</form>
</body>
</html>

success.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作成功</title>
</head>
<body>
	注册成功!!!
</body>
</html>

Five to create a startup class test

package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

Six summary

   To reproduce the error

 

    The following error may occur during testing:

 

Solution:

  • The variable name to class name first letter lowercase, for example, a variable named users Users
  • Use @ModelAttribute ( "u") Custom Variables

Guess you like

Origin blog.csdn.net/qq_33223299/article/details/90674057