十九 SpringBoot服务端表单数据校验-实现添加用户功能

十九 SpringBoot服务端表单数据校验-实现添加用户功能

1 创建maven项目

十九 SpringBoot服务端表单数据校验-实现添加用户功能

十九 SpringBoot服务端表单数据校验-实现添加用户功能

 

2 修改pom.xml文件

<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>2.1.3.RELEASE</version>
	</parent>
	<groupId>com.bjsxt</groupId>
	<artifactId>13-spring-boot-validate</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	
	<dependencies>
		<!-- springboot的web启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- springboot的thymeleaf启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>
</project>

3 添加用户页面

3.1 addUser.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
	<form th:action="@{/saveUser}" method="post">
		用户姓名:<input type="text" name="name"/><br/><br/>
		用户密码:<input type="password" name="password"/><br/><br/>
		用户年龄:<input type="text" name="age"/><br/><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

3.2 ok.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<body>
	操作成功
</body>
</html>

4 创建用户实体类Users.java

package com.bjsxt.pojo;

public class Users {
	private String name;
	private String password;
	private Integer age;
	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;
	}
	public Users(String name, String password, Integer age) {
		super();
		this.name = name;
		this.password = password;
		this.age = age;
	}
	public Users() {
		super();
	}
	@Override
	public String toString() {
		return "Users [name=" + name + ", password=" + password + ", age=" + age + "]";
	}
}

5 编写controller

@Controller
public class UsersController {
	@RequestMapping("/addUser")
	public String addUser() {
		return "addUser";
	}
	
	@RequestMapping("/saveUser")
	public String saveUser(Users users) {
		return "ok";
	}
}

6 编写启动类

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

7 启动测试

十九 SpringBoot服务端表单数据校验-实现添加用户功能