表单验证

给表单添加验证的步骤如下

1.在 pom.xml 里添加 hibernate-validator 依赖
http://hibernate.org/validator/documentation/

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.3.Final</version>
</dependency>

注意版本的问题,在 Maven 依赖小计中说过。

2.给表单实体类添加注解

public class PersonForm {

@NotNull
@Size(min=2, max=30, message = "{personForm.name}")
private String name;

@NotNull
@Min(18)
private Integer age;
}

3.修改控制器的方法
https://spring.io/guides/gs/validating-form-input/#_create_a_web_controller

@PostMapping("/")
public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult) {

if (bindingResult.hasErrors()) {
return "form";
}

return "redirect:/results";
}

(1) 用 @Valid 标记表单对象参数
(2) 紧挨着 personForm 加一个 BindingResult bindingResult

4.在JSP页面上添加显示错误的项

用 <form:errors> 标签在页面上显示错误信息

<td><form:input path="name"/><form:errors path="name"/></td>

ps.本地化,把错误提示信息放在资源文件中

添加 /src/main/resources/ValidationMessages.properties 文件

personForm.name = 用户名限制 {min} ~ {max} 个字符

错误信息中文乱码的问题
https://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle

使用 native2ascii 命令转换文件编码
native2ascii -encoding UTF-8 text_utf8.properties text.properties

在 Intellij IDEA 中的操作
https://www.jetbrains.com/help/idea/properties-files.html

在 Settings->Editor->File Encodings 中勾选 Transparent native-to-ascii conversion 。

设置好后IDEA的编辑器会自动处理文件的编码和解码。

Spring表单验证的更多相关文章

  1. Spring表单验证(Spring Validation)

    1.基本介绍 之前在项目中做的后台验证就是Spring Validation,最近闲下来了,就来整理一下. 从Spring3.0开始,Spring MVC中提供了对java校验的API支持.在Spri ...

  2. Spring进行表单验证

    转自:https://www.tianmaying.com/tutorial/spring-form-validation 开发环境 IDE+Java环境(JDK 1.7或以上版本) Maven 3. ...

  3. 玩转spring boot——AOP与表单验证

    AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...

  4. spring mvc:练习:表单验证(javaConfig配置和注解)

    使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS,JavaScript,图片) ...

  5. spring+thymeleaf实现表单验证数据双向绑定

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

  6. Spring Boot学习——表单验证

    我觉得表单验证主要是用来防范小白搞乱网站和一些低级的黑客技术.Spring Boot可以使用注解 @Valid 进行表单验证.下面是一个例子. 例子说明:数据库增加一条Student记录,要求学生年龄 ...

  7. Spring Boot 2 + Thymeleaf:服务器端表单验证

    表单验证分为前端验证和服务器端验证.服务器端验证方面,Java提供了主要用于数据验证的JSR 303规范,而Hibernate Validator实现了JSR 303规范.项目依赖加入spring-b ...

  8. Spring Boot笔记八:表单验证

    所谓的表单验证,就是为了防止用户乱输入的,这个问题前端的HTML5就可以判断了,其实不需要后端来验证,这里还是讲一下后端验证 首先,我们的Person类,我们加上一些表单验证的注释,如下: packa ...

  9. spring boot学习(7) SpringBoot 之表单验证

    第一节:SpringBoot 之表单验证@Valid 是spring-data-jpa的功能:   下面是添加学生的信息例子,要求姓名不能为空,年龄大于18岁.   贴下代码吧: Student实体: ...

随机推荐

  1. Maven的pom.xml介绍

    6.1 简介 pom.xml文件是Maven进行工作的主要配置文件.在这个文件中我们可以配置Maven项目的groupId.artifactId和version等Maven项目必须的元素:可以配置Ma ...

  2. smarty if

    <{if data}> <input type="submit" value="修改" /> <{else}> <in ...

  3. 2.QWidget类

    简述: QWidget类是所有用户界面对象的基类. Widget是用户界面的基本单元:它从窗口系统接收鼠标,键盘和其他事件,并在屏幕上绘制自己. 每个Widget都是矩形的,它们按照Z-order进行 ...

  4. [Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] HashSet and Hashtable

    Hashset: HashSet set = new HashSet() set.add("India") set.add("USA") set.add(&qu ...

  5. centos7 dubbokeeper安装

    下载dubbokeeper源码 git clone https://github.com/dubboclub/dubbokeeper   mysql 先执行install-mysql.sh   编译好 ...

  6. ASP.Net Web API 输出缓存(转)

    出处:http://www.cnblogs.com/ajilisiwei/p/6112078.html 原文的转载地址:http://www.strathweb.com/2012/05/output- ...

  7. 5 Django-2 的路由层 (URLconf)

    URL 配置 (URLconf) 就像 Django 所支撑网站的目录.它的本质是 URL 与要为该 URL 调用的视图函数之间的映射表:你就是以这种方式告诉 Django,对于客户端发来的某个 UR ...

  8. word 2007 写CSDN博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  9. 看图说说JVM GC收集算法

  10. CodeForces - 468A ——(思维题)

    Little X used to play a card game called "24 Game", but recently he has found it too easy. ...