Spring Boot教程(六)在springboot中验证表单信息
构建工程
创建一个springboot工程,由于用到了 web 、thymeleaf、validator、el,引入相应的起步依赖和依赖,代码清单如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</dependency>
</dependencies>
创建一个PresonForm的Object类
package com.forezp.entity; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
* Created by fangzhipeng on 2017/4/19.
*/
public class PersonForm { @NotNull
@Size(min=2, max=30)
private String name; @NotNull
@Min(18)
private Integer age; public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String toString() {
return "Person(Name: " + this.name + ", Age: " + this.age + ")";
}
}
这个实体类,在2个属性:name,age.它们各自有验证的注解:
- @Size(min=2, max=30) name的长度为2-30个字符
- @NotNull 不为空
- @Min(18)age不能小于18
创建 web Controller
@Controller
public class WebController extends WebMvcConfigurerAdapter { @Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/results").setViewName("results");
} @GetMapping("/")
public String showForm(PersonForm personForm) {
return "form";
} @PostMapping("/")
public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult) { if (bindingResult.hasErrors()) {
return "form";
} return "redirect:/results";
}
}
创建form表单
src/main/resources/templates/form.html:
<html>
<body>
<form action="#" th:action="@{/}" th:object="${personForm}" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{name}" /></td>
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" th:field="*{age}" /></td>
<td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Age Error</td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table></form></body></html>
注册成功的页面
src/main/resources/templates/results.html:
html>
<body>
Congratulations! You are old enough to sign up for this site.
</body>
</html>

源码来源
Spring Boot教程(六)在springboot中验证表单信息的更多相关文章
- 在springboot中验证表单信息(六)
构建工程 创建一个springboot工程,由于用到了 web .thymeleaf.validator.el,引入相应的起步依赖和依赖,代码清单如下: 1 2 3 4 5 6 7 8 9 10 11 ...
- SpringBoot非官方教程 | 第十九篇: 验证表单信息
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot19/ 本文出自方志朋的博客 这篇文篇主要简述如何 ...
- flask 在视图函数中验证表单
在视图函数中验证表单 因为现在的basic_form视图同时接受两种类型的请求:GET请求和POST请求.所以我们要根据请求方法的不同执行不同的代码.具体来说,首先是实例化表单,如果是GET请求,就渲 ...
- ThinkPHP中create()方法自动验证表单信息
自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 原理: create()方法收集表单($_POST)信息并返回,同时触发表单自动验证 ...
- 使用selenium遍历frame中的表单信息 ;
遍历frame中的表单 : package webDriverPro; import java.util.List; import java.util.regex.Matcher; import ja ...
- laravel 验证表单信息
1控制器验证 $this->validate($request,[ 'Student.name'=>'required|min:2|max:20', 'Student.age'=>' ...
- spring boot 学习(七)小工具篇:表单重复提交
注解 + 拦截器:解决表单重复提交 前言 学习 Spring Boot 中,我想将我在项目中添加几个我在 SpringMVC 框架中常用的工具类(主要都是涉及到 Spring AOP 部分知识).比如 ...
- jquery 验证表单信息
/** * $().validate(json); * *rules:自定义规则 * *messages:提示信息 */ $(document).ready(function(){ $(". ...
- ValidForm验证表单
在做项目时,要求熟悉项目中验证表单的插件,所以学习一下validForm这个插件 http://validform.rjboy.cn/document.html#validformObject
随机推荐
- PostgreSQL逻辑复制到kafka-实践
kafka 安装 wget http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz cp kafka_2.12-2.0.1.t ...
- Being a Good Boy in Spring Festival
Being a Good Boy in Spring Festival Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一 ...
- MySQL总结(5)
视图 SELECT cust_name,cust_contact FROM customers,orders,orderitems WHERE customers.cust_id=orders.cus ...
- 生成url的二维码图片
<?php require_once "./phpqrcode.php"; //生成二维码 $img = \QRcode::png("https://www.bai ...
- 对Elastic集群内部配置TLS加密通信及身份验证
1.介绍 官方宣布从6.8和7.1开始,免费提供多项安全功能.其中包括tls加密通信,基于角色访问控制等功能. 可以使用企业CA证书来完成这一步骤,但是一般情况下,我们可以通过elasticsearc ...
- ElasticSearch5.3安装IK分词器并验证
ElasticSearch5.3安装IK分词器 之前使用Elasticsearch安装head插件成功了,但是安装IK分词器却失败了.貌似是ElasticSearch5.0以后就不支持直接在elast ...
- JS作用域及域解析规则
1.JS作用域:变量和函数作用的范围. 2.JS解析器可以分为域解析和逐行解读代码两个过程. 域解析:1.当进行域解析的时候,一旦找到var,就会提取后面的变量名,并给它赋值给undefined. 2 ...
- JS 的 Element元素对象
在 HTML DOM 中, 元素对象代表着一个 HTML 元素. 元素对象 的 子节点可以是, 可以是元素节点,文本节点,注释节点. NodeList 对象 代表了节点列表,类似于 HTML元素的子节 ...
- java 序列化原来如此
上次面试的时候 ,如何实现java 类的序列化,当时感觉这个问题很简单,我的回答是实现serizlizable 接口就好了,可以实现对象的持久化,看了看书,原来这样: public class Ser ...
- MathType 7.4.2.480
目录 1. 相关推荐 2. 按 3. 软件介绍 4. 安装步骤 5. 使用说明 6. 下载地址 1. 相关推荐 推荐使用:AxMath(AxMath可以与LaTeX进行交互):https://blog ...