当前位置: 首页 > news >正文

凤山县住房和城乡建设局网站关键字搜索软件

凤山县住房和城乡建设局网站,关键字搜索软件,深圳推广公司哪家最好,做网站代理商好赚吗?在现代的Web开发中,数据验证是一个不可或缺的环节。它不仅可以保证数据的合法性,还能提升用户体验。Spring Boot结合JSR 303/349/380 Bean Validation API以及Thymeleaf模板引擎,为我们提供了一种高效且优雅的方式来实现数据验证。本文将通过…

在现代的Web开发中,数据验证是一个不可或缺的环节。它不仅可以保证数据的合法性,还能提升用户体验。Spring Boot结合JSR 303/349/380 Bean Validation API以及Thymeleaf模板引擎,为我们提供了一种高效且优雅的方式来实现数据验证。本文将通过一个简单的员工信息管理系统的实例,详细介绍如何在Spring Boot中使用Bean Validation API进行数据验证。

一、Bean Validation API简介

Bean Validation API(JSR 303/349/380)是一种用于Java Bean验证的规范,它允许我们通过注解的方式在Java类的字段上定义验证规则。这些注解在运行时会被验证框架解析,并对不符合规则的数据进行拦截和提示。在Spring Boot中,我们通常使用Hibernate Validator作为Bean Validation的实现。

二、实例:员工信息管理系统

假设我们正在开发一个员工信息管理系统,需要对员工的姓名、部门和出生日期进行验证。以下是实现该功能的具体步骤。

1. 定义Employee实体类

首先,我们需要定义一个Employee类,用于表示员工信息。在这个类中,我们将使用Bean Validation注解来定义字段的验证规则。

package com.logicbig.example;import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.time.LocalDate;public class Employee {@NotNull(message = "Name cannot be null")@Size(min = 5, max = 50, message = "Name must be between 5 and 50 characters")private String name;@Pattern(regexp = "Admin|IT|Sales|Accounts", message = "Invalid department")private String dept;@Past(message = "Date of birth must be in the past")@NotNull(message = "Date of birth cannot be null")@DateTimeFormat(pattern = "yyyy-MM-dd")private LocalDate dateOfBirth;// Getters and Setters
}

在上述代码中,我们使用了以下注解:

  • @NotNull:字段不能为空。
  • @Size:字段的长度必须在指定范围内。
  • @Pattern:字段的值必须符合正则表达式。
  • @Past:日期字段必须是过去的日期。
  • @DateTimeFormat:用于将字符串格式化为日期对象。

2. 创建MVC控制器

接下来,我们需要创建一个控制器来处理员工信息的提交和展示。

@Controller
public class EmployeeController {private static List<Employee> employeeList = new ArrayList<>();@PostMapping("/")public String handlePostRequest(@Valid Employee employee, BindingResult bindingResult) {if (bindingResult.hasErrors()) {return "employee-form";}employeeList.add(employee);return "redirect:/employees";}@GetMapping("/")public String handleGetRequest(Model model) {model.addAttribute("employee", new Employee());return "employee-form";}@GetMapping("/employees")public String handleGetRequest(Model model) {model.addAttribute("employees", employeeList);return "employee-view";}
}

handlePostRequest方法中,我们使用了@Valid注解来触发验证。如果验证失败,BindingResult会包含错误信息,并将用户重定向回表单页面。

3. Thymeleaf视图

为了展示表单和验证错误信息,我们需要创建两个Thymeleaf模板文件。

employee-form.html
<html>
<head>
<style>
table.emp-form td:nth-child(3){color:red;}
</style>
</head>
<body>
<form action="#" th:action="@{/}" th:object="${employee}" method="post"><table class="emp-form"><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>Department:</td><td><input type="text" th:field="*{dept}"/></td><td th:if="${#fields.hasErrors('dept')}" th:errors="*{dept}">Department Error</td></tr><tr><td>Date Of Birth:</td><td><input type="text" th:field="*{dateOfBirth}"/></td><td th:if="${#fields.hasErrors('dateOfBirth')}" th:errors="*{dateOfBirth}">Date Of Birth Error</td></tr><tr><td><button type="submit">Submit</button></td></tr></table>
</form>
</body>
</html>
employee-view.html
<html>
<head>
<style>
table.emp-table{width:100%;}
table.emp-table td {border:solid 1px #aaa;}
table.emp-table th {border:solid 1px #aaa; background: #bbb;}
</style>
</head>
<body><h3>Saved Employees</h3><table class="emp-table"><tr><th>Name</th><th>Department</th><th>Date Of Birth</th></tr><tr th:each="employee : ${employees}"><td th:text="${employee.name}"></td><td th:text="${employee.dept}"></td><td th:text="${employee.dateOfBirth}"></td></tr></table><br/><a th:href="@{/}">Add new employee</a>
</body>
</html>

4. 运行项目

运行项目后,访问http://localhost:8080/,即可看到员工信息表单页面。提交表单时,如果输入的数据不符合验证规则,页面会显示相应的错误信息。

三、总结

通过上述实例,我们展示了如何在Spring Boot中使用Bean Validation API结合Thymeleaf模板引擎实现数据验证。这种方式不仅简单易用,还能有效提升开发效率和用户体验。希望本文能为你的Spring Boot项目提供一些参考和帮助。

http://www.cadmedia.cn/news/1088.html

相关文章:

  • 品牌网站都有哪些北京seo网站设计
  • 湖南网站建设公司 干净磐石网络十大软件免费下载网站排行榜
  • 手机选择网站市场营销互联网营销
  • 山西省建设厅网站打不开生成关键词的软件
  • 学校网站设计论文二十条优化措施原文
  • 今日油价95号汽油价格湖南网站seo地址
  • 山东城市建设招生网站百度竞价被换着ip点击
  • 成都企业网站建设那家好排名首页服务热线
  • wordpress header.php在哪里seo网站优化收藏
  • 东莞品牌网站建设网页制作步骤
  • 济南将开展治堵十大行动seo优化服务商
  • 网站建设"淘宝网" 在颜色选取和搭配方面有哪些值得学习的地方.在线seo优化工具
  • 泗阳网站定制公司网站的推广方案
  • 网站做中秋专题怎么弄郑州关键词排名公司电话
  • 交友网站建设要多少钱百度代运营公司
  • 一建 建设网站首页seo诊断站长
  • 大悟网站设计免费产品推广软件
  • 望京网站建设公司营销活动推广方案
  • 口碑好的福州网站建设网站建设关键词排名
  • 开发一个网站需要的技术百度应用宝
  • 网络绿化网站建设哪家专业百度店铺免费入驻
  • 公司做网站要注意什么淘宝推广运营
  • 网站设计与建设公司百度服务中心
  • 装修公司网站该怎么做站长工具seo综合查询分析
  • 手机网站建设咨询网络推广外包联系方式
  • seo关键词怎么优化seo网络推广哪家专业
  • 北京网络营销的培训课程淄博seo
  • 深圳做棋牌网站建设哪家服务好百度上做推广怎么收费
  • 郑州建设企业网站找哪个公司关键词优化和seo
  • 怎样申请注册公司网站整合营销活动策划方案