springBoot数据校验

springBoot 数据校验

1配置pom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.3.1.RELEASE</version>-->
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.study</groupId>
    <artifactId>study-validate</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>study-validate</name>
    <description>Demo project for Spring Boot</description>

    <!--    使用版本2.0以上jdk要用1.8以上的版本-->
    <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 的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency> <!-- springBoot 的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

</project>

2创建启动类

/**
 * SpringBoot启动类
 *
 *
 */
@SpringBootApplication
public class App {

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

3编写实体类

public class User {
    @NotBlank(message="用户名不能为空") //非空校验
    @Length(min=2,max=6,message="最小长度为2位,最大长度为6位")
    private String name;
//    @NotBlank //密码非空校验
    @NotEmpty
    private String password;
    @Min(value=15)
    private Integer age;
    @Email
    private String 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;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

4编写控制层

/**
 * SpringBoot 数据校验
 * 
 */
@Controller
public class UserController {

    @RequestMapping("/addUser")
    public String showPage(User user) {
        return "add";
    }

    /*** 完成用户添加 */
    @RequestMapping("/save")
    public String saveUser(@Valid User user, BindingResult result) {
        if (result.hasErrors()) {
            return "add";
        }
        System.out.println(user);
        return "ok";
    }
}

4编写 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="${user.name}"></font><br/>
    用户密码:<input type="password" name="password" /><font color="red" th:errors="${user.password}"></font><br/>
    用户年龄:<input type="text" name="age" /><font color="red" th:errors="${user.age}"></font><br/>
    用户邮箱:<input type="text" name="email" /><font color="red" th:errors="${user.email}"></font><br/>
    <input type="submit" value="OK"/></form>
</body>
</html>

5编写 ok.html

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

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值