构建工程
创建一个springboot工程,由于用到了 web 、thymeleaf、validator、el,引入相应的起步依赖和依赖,代码清单如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<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类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<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:
1
2
3
4
5
|
html> <body> Congratulations! You are old enough to sign up for this site. </body> </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具