文章目录
- 前言
- 一、准备
- 1. 引入库
- 2. add.jsp
- 3. show.jsp
- 二、代码部分
- 1.实体类
- 2. 控制器类
- 3. 效果
- 4. 展示
- 总结
前言
【第20章】spring-validator
虽然前面已经在spring介绍过,但是为了保证代码可用,还是会从头讲到尾,尽量把关键点列出来讲给大家。
一、准备
1. 引入库
<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator --><dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId><version>8.0.1.Final</version></dependency><!-- https://mvnrepository.com/artifact/org.glassfish/jakarta.el --><dependency><groupId>org.glassfish</groupId><artifactId>jakarta.el</artifactId><version>4.0.2</version><scope>test</scope></dependency>
2. add.jsp
<%--Created by IntelliJ IDEA.User: 张军国001Date: 2024/5/2Time: 17:53To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>增加用户</title>
</head>
<body>
<form action="/user" method="post"><span>姓名:</span><input name="name">${name}<br><span>年龄:</span><input name="age">${age}<br><span>出生日期:</span><input name="brith">${brith}<br><span>手机号:</span><input name="phone">${phone}<br><span>邮箱:</span><input name="email">${email}<br><button type="submit">增加</button>
</form>
</body>
</html>
3. show.jsp
<%--Created by IntelliJ IDEA.User: 张军国001Date: 2024/5/2Time: 17:53To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<head><title>结果展示</title>
</head>
<body>
<span>姓名:</span>${user.name}<br>
<span>年龄:</span>${user.age}<br>
<span>出生日期:</span><spring:eval expression="user.brith"/><br>
<span>手机号:</span>${user.phone}<br>
<span>邮箱:</span>${user.email}<br>
</body>
</html>
二、代码部分
1.实体类
package org.example.springmvc.validator.po;import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Past;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;/*** Create by zjg on 2024/5/2*/
@Getter
@Setter
@ToString
public class User {@NotEmpty(message = "name is not null")private String name;@NotNull(message = "Age is not null")@Min(value=0,message = "Age range required to be between 0-130")@Max(value=130,message = "Age range required to be between 0-130")private Integer age;@NotNull(message = "brith is not null")@DateTimeFormat(pattern = "yyyyMMdd")@Past(message = "Birth date must be before the current date")private Date brith;@NotEmpty(message = "phone is not null")@Pattern(regexp = "^1[3-9]\\d{9}$",message="请输入11位正确的手机号码")private String phone;@Email@NotEmpty(message = "email is not null")private String email;
}
2. 控制器类
package org.example.springmvc.validator.controller;import jakarta.validation.Valid;
import org.example.springmvc.validator.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;/*** Create by zjg on 2024/5/2*/
@Controller
public class ValidatorController {@RequestMapping("hello-vaildator")public String hello(){return "validator/add";}@PostMapping("/user")public String add(@Valid User user, BindingResult bindingResult, Model model){System.out.println(user);if(bindingResult.hasFieldErrors()){for (FieldError fieldError:bindingResult.getFieldErrors()){model.addAttribute(fieldError.getField(),fieldError.getDefaultMessage());}return "validator/add";}return "validator/show";}
}
3. 效果
4. 展示
总结
回到顶部
使用起来比spring要简单些,希望能帮助到大家。