首页 > 其他分享 >SpringBoot(十):thymeleaf + JSR303实现表单验证

SpringBoot(十):thymeleaf + JSR303实现表单验证

时间:2023-03-06 20:56:01浏览次数:41  
标签:String Person JSR303 private person thymeleaf import public SpringBoot

一、Person类

package com.jms.pojo;

import lombok.Data;

import javax.validation.constraints.*;

@Data
public class Person {

    @NotBlank(message = "姓名不能为空")
    private String name;
    @Max(value = 100, message = "年龄不能超过100")
    @Min(0)
    private String age;
    private String phone;
    private String email;

}

二、Controller

package com.jms.controller;

import com.jms.pojo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Controller
public class PersonController {

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("person", new Person());
        return "index";
    }

    @PostMapping("/")
    public String getPerson(@Valid Person person, Errors errors) {
        if (errors.hasErrors()) return "index";
        else return "person";
    }
}

三、thymeleaf页面

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>主页面</title>
</head>
<body>
<form method="post" th:action="@{/}" th:object="${person}">
    <div>
        <input type="text" th:field="*{name}">
        <span th:if="${#fields.hasErrors('name')}" th:errors="*{name}">错误提示</span>
    </div>
    <div>
        <input type="text" th:field="*{age}">
        <span th:if="${#fields.hasErrors('age')}"
              th:errors="*{age}">
        </span>
    </div>
    <div>
        <input type="text" th:field="*{phone}">
        <span th:if="${#fields.hasErrors('phone')}"
              th:errors="*{phone}">
        </span>
    </div>
    <div>
        <input type="text" th:field="*{email}">
        <span th:if="${#fields.hasErrors('email')}"
              th:errors="*{email}">
        </span>
    </div>
    <input type="submit" value="提交">
</form>
</body>

</html>

person.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>person</title>
</head>
<body>
  <P>没有错误</P>
</form>
</body>
</html>

四、测试

 

 没有错误后成功跳转person.html

 

(本文仅作个人学习记录用,如有纰漏敬请指正)

标签:String,Person,JSR303,private,person,thymeleaf,import,public,SpringBoot
From: https://www.cnblogs.com/jmsstudy/p/17185399.html

相关文章

  • SpringBoot中基于拦截器实现登录验证功能
    拦截器简介拦截器是属于springmvc体系的,只能拦截controller的请求。拦截器(Interceptor)是一种动态拦截方法调用的机制,在SpringMVC中动态拦截控制器方法的执行。Interceptor......
  • springboot项目jar包混淆加密
    混淆工具jar包混淆工具挺多的,实现原理不尽相同,这里使用的classfinal加密执行以下命令java-jarclassfinal-fatjar.jar-fileyourproject.jar-libjarsa.jar,b.jar-......
  • Springboot上传并解析Zip包
    packagecom.c2f.hbos.mcc.common.utils;importcom.c2f.hbos.mcc.core.catalog.enums.SHMedicationCatalogDataSourceEnum;importorg.apache.commons.compress.utils.......
  • SpringBoot+Vue中使用AES进行加解密(加密模式等对照关系)
    场景若依前后端分离版本地搭建开发环境并运行项目的教程:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662在上面搭建起来前后端架构之后,在前后端......
  • (转帖)SpringBoot自动装配的原理
    自动装配:就是把自动去把第三方组件的Bean装载到IOC容器里,而不需要开发人员在去写相关的Bean的一个配置。在SpringBoot项目中只需要加@SpringBootApplication的一个注解就......
  • springboot集成nacos配置中心
    springboot版本2.6.7+nacos版本2.1.21、添加nacos依赖com.alibaba.bootnacos-config-spring-boot-starter0.2.122、配置application.propertiesnacos.config.boots......
  • springboot 集成feign 实现远程调用
    1. springboot项目引入pom相关依赖<dependency><groupId>io.github.openfeign</groupId><artifactId>feign-core</artifactId>......
  • springboot中redis的基本数据类型测试
    在springboot中依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artif......
  • SpringBoot开发实用-默认数据源
    数据源技术​ 目前我们使用的数据源技术是Druid,运行时可以在日志中看到对应的数据源初始化信息,具体如下:INFO28600---[main]c.a.d.s.b.a.DruidDataSource......
  • 已解决 springBoot HttpMessageConversionException的异常
    问题说明:近日重构springboot项目,启动后调用ControllerAPI异常提示:2022-12-0617:09:37.008javaERROR[http-nio-8080-exec-2]o.a.c.c.C.[.[.[.[dispatcherServlet]e......