Spring MVC 表单验证
1. 基于 JSR-303(一个数据验证的规范):
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank; public class User { private String username; private String password; private int age; @NotBlank(message="用户名不能为空")
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} @NotNull(message="密码不能为null")
public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Min(value=10, message="年龄的最小值为10")
public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven/>
</beans>
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class UserController { @RequestMapping("login")
public String login(@Valid User user, BindingResult result) {
if (result.hasErrors())
return "user/login";
return "redirect:/";
} }
使用@NotBlank等注解,然后在要验证的地方使用@Valid 标签。最后验证的结果是在BindingResult 获取,注意据说result 要紧挨着前面的@Valid,而且是一个验证一个BindingResult参数。
2. 还有就是继承Validator 接口。
Spring MVC 表单验证的更多相关文章
- Spring MVC - 表单处理示例
环境搭建 环境: Intellij IDEA Spring MVC 完整的项目文件结构如下所示: Student.java package com.ktao.controller; public cl ...
- Spring MVC表单处理
以下示例演示如何编写一个简单的基于Web的应用程序,它使用Spring Web MVC框架使用HTML表单. 首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework ...
- 使用Spring MVC表单标(转)
概述 在低版本的Spring中,你必须通过JSTL或<spring:bind>将表单对象绑定到HTML表单页面中,对于习惯了Struts表单标签的开发者来说,Spring MVC的 ...
- Spring MVC表单标签
从Spring 2.0开始,Spring MVC开始全面支持表单标签,通过Spring MVC表单标签,我们可以很容易地将控制器相关的表单对象绑定到HTML表单元素中. form标签 和使用任 ...
- Spring MVC表单提交
实际应用中,列表中的单条记录的修改,可能需要传很多对象参数到后台服务器,Spring MVC表单标签<form:> 提供了一种简洁的提交方式. <form id="form ...
- Spring进行表单验证
转自:https://www.tianmaying.com/tutorial/spring-form-validation 开发环境 IDE+Java环境(JDK 1.7或以上版本) Maven 3. ...
- Spring4 MVC表单验证
在这篇文章中,我们将学习如何使用Spring表单标签, 表单验证使用 JSR303 的验证注解,hibernate-validators,提供了使用MessageSource和访问静态资源(如CSS, ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(33)-MVC 表单验证
系列目录 注:本节阅读需要有MVC 自定义验证的基础,否则比较吃力 一直以来表单的验证都是不可或缺的,微软的东西还是做得比较人性化的,从webform到MVC,都做到了双向验证 单单的用js实现的前端 ...
- Spring MVC 表单校验 (七)
完整的项目案例: springmvc.zip 目录 实例 除了依赖spring-webmvc还需要依赖jackson-databind(用于转换json数据格式)和hibernate-validato ...
随机推荐
- 【译】Envoy with Nomad and Consul (一)
原文: http://timperrett.com/2017/05/13/nomad-with-envoy-and-consul 在过去的许多年我的职业生涯一直是围绕着数据中心和平台基础设施.工作范围 ...
- 全景智慧城市常诚——一个实体商家“自剖”VR全景的势在必得
谈起"智慧城市",你的心中是否充满了期待?随着互联网的发展,人们对于"智慧城市"的需求越来越迫切.现在想想,我也算是首批入驻全景智慧城市的商家之一了.在各种连锁 ...
- poj3683
poj3683 题意 n对新人举行婚礼,婚礼在不同时间段但可能重叠,婚礼有开始(Si).结束(Ti).仪式举行时间(Di),问能否给出一种举行方案,使得神父能参加所有的婚礼并举行仪式. 分析 xi为真 ...
- Java阶段性测试--第二三大题参考代码
第二大题: 1.打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于它本身 package Test1; //1.打印出所有的 ...
- Selenium基础知识
本人博客文章网址:https://www.peretang.com/basic-knowledge-of-selenium/ 什么是Selenium Selenium是一个自动化测试工具 是一组不同的 ...
- 创建的docker容器时间显示错误/date错误/时区错误
前几天在测试应用的功能时,发现存入数据库中的数据create_time或者update_time字段总是错误,其他数据都是正常的,只有关于时间的字段是错误的. 进入linux服务器中查看,也没有任何的 ...
- postgresql 多表联查
使用语句的先后顺序并不是优先级的排序: 连接分为:内连接和外连接,外连接分为左外连接,右外连接,全连接 概念上解释,表之间联合后数据如何整合. 返回的数据条数,可以通过集合求算.假如A集合有10条数据 ...
- 微信小程序开发 -- 01
微信小程序开发基础 -- 开发前的准备 缘由 1月9日张小龙微信小程序正式上线,因为微信,所以小程序从诞生开始就头戴巨大的光环,很多的团队,公司以及开发的个体都眼巴巴的盯着这个小程序.而那个时候我却在 ...
- iOS,Android,Jave后台AES加密解密
AES256 在iOS和Android上的相关代码: http://www.tuicool.com/articles/RVFbmmU 里面可以下载相关的代码. 我们遇到的问题是: 把Android的代 ...
- 编写原生JS的insertAfter函数
DOM里有insertBefore函数,但没有insertAfter函数,所以自己编写一个该函数: function insertAfter(newElement, targetElement){ v ...