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

漳州公司建设网站吸引人的营销标题

漳州公司建设网站,吸引人的营销标题,商城式网站具备哪些功能吗,保安做网站在现代的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.dtcms.com/wzjs/6296.html

相关文章:

  • 建材网站做环保类型思路引流黑科技app
  • 做市级网站需要什么深圳优化排名公司
  • 有没有做微信的动态图网站产品营销方案策划
  • 年底 网站备案广点通官网
  • 百合网网站建设与策划微信小程序开发零基础入门
  • 网站建设套模网站营销策划
  • 政府网站页面布局评价日本产品和韩国产品哪个好
  • 全球设计网网址优化一下
  • 怎么做网站页面模板安徽搜索引擎优化seo
  • 西安网站制作顶尖公西安百度爱采购推广
  • 买网站做设计参考属于什么费用seo营销方法
  • 兰州网站排名外包小程序开发哪家好
  • 德州哪个做网站做得好seo优质友链购买
  • dw做网站怎样插入表单网店怎么推广和宣传
  • 关于网站建设的题目焦作关键词优化排名
  • 自己做的网站不能用手机访问有哪些免费推广软件
  • 做的网站怎么进后台什么是搜索引擎优化?
  • 电影网站做视频联盟怎么建企业网站
  • 做电商的网站泉州关键词搜索排名
  • 做海报有什么素材网站知乎广州seo软件
  • 做网站和软件有区别吗网站seo是啥
  • 做网站有哪些导航条汕头网站制作设计
  • 建设银行 网站 字体自己怎么做网址
  • 什么网站做优化最好苏州网站建设书生商友
  • 网站建设服务亮点网上有卖网站链接的吗
  • 关于美食网站的问卷调查怎么做中国seo高手排行榜
  • 手机商城建站系统网站建设南昌企业网站建设
  • 禅城区城乡建设局网站搜索引擎seo优化怎么做
  • 代理网页 国外优化seo是什么
  • 可以做彩票广告的网站吗网站seo优化方案策划书