Spring中的DataBinding(二) - Validation
@Controller
@RequestMapping(value = "/custom/register")
public class RegistrationController {
// Set the data binding per controller
@InitBinder
public void initBinder(WebDataBinder binder){
binder.setDisallowedFields("id");
// 此处指定在绑定的时候存在两个必须字段
binder.setRequiredFields("userName","password");
}
// POST请求的时候将BindingResult的对象置于参数列表中。 BindingResult是<form: error> 这个tag的子类。
@RequestMapping(method = {RequestMethod.POST,RequestMethod.PUT})
public String handleRegistration(Account account, BindingResult result){
// 校验过程中如果存在error返回到本页,而且本页中有一个DataModel为Result
if(result.hasErrors())
return "custom/register"; accountService.SaveAccount(account);
return "redirect:custom/account/"+account.getId();
}
}
JSP文件中,在相应的控件处添加Error 标签,这样如果BindingResult不为空。这些Error 标签就会将提示信息显示出来
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2015/4/6
Time: 22:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<form:form modelAttribute="account" method="post">
<fieldset>
<legend>
Account Personal Information
</legend>
<table>
<tr>
<td>FirstName</td>
<td>
<form:input path="firstName"></form:input>
</td> </tr>
<tr>
<td>LastName</td>
<td>
<form:input path="lastName"></form:input>
</td>
</tr> </table>
</fieldset>
<fieldset>
<legend>Account information</legend> <table>
<tr>
<td>User Name</td>
<td><form:input path="userName"></form:input></td>
<td><form:errors path="userName"></form:errors> </td>
</tr>
<tr>
<td><form:label path="password">Password</form:label></td>
<td><form:input path="password"></form:input></td>
<td><form:errors path="password"></form:errors> </td>
</tr> </table>
</fieldset> <form:button value="Submit">提交 </form:button>
</form:form>
</body>
</html>
Spring中的DataBinding(二) - Validation的更多相关文章
- Spring中的DataBinding(一)
DataBinding在Spring中应用. 第一点:使用ModelAttribute指定带绑定的数据内容 很重要的属性:@ModelAttribute([value=""])可以 ...
- spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)
在上篇博客中分享了InstantiationAwareBeanPostProcessor接口中的四个方法,分别对其进行了详细的介绍,在文末留下了一个问题,那就是postProcessPropertie ...
- spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)
在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...
- spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)
在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- spring boot高性能实现二维码扫码登录(中)——Redis版
前言 本打算用CountDownLatch来实现,但有个问题我没有考虑,就是当用户APP没有扫二维码的时候,线程会阻塞5分钟,这反而造成性能的下降.好吧,现在回归传统方式:前端ajax每隔1秒或2秒发 ...
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- Spring学习(二)——Spring中的AOP的初步理解
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入
<spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...
随机推荐
- linux下挂载CDROM命令
cdrom装载在/dev/cdrom 下面. 挂载时在权限范围内建立一个文件夹.例如mkdir /home/dang/cdtmp 然后执行 mount dev/cdrom /home/dang/cdt ...
- 我和Python的第一次亲密接触 - 零基础入门学习Python001
我和Python的第一次亲密接触 让编程改变世界 Change the world by program 小甲鱼经常说,学习一门编程语言就是要深入其中,要把编程语言想象为你的女朋友你的情人,这样你学习 ...
- information_schema.collations 学习
information_schema.collations 表中的每一行对应一个排序规则 1.information_schema.collations 表中学用列: 1.id :排序规则的ID 2. ...
- soap和http(转)
http:是一个客户端和服务器端请求和应答的标准(TCP).http协议其目的是为了提供一种发布和接收http页面的方法 一 http协议的客户端与服务器的交互:由HTTP客户端发起一个请求,建立一个 ...
- linux环境下deb格式文件转换成rpm格式
以 alien_8.87.tar.gz 为例: 下载.安装 alien_8.87.tar.gz [root@shyn ~]# wget http://ftp.de.debian.org/debian/ ...
- SPI总线的4种工作模式
spi总线的4种工作模式 0 to 4 modes SPI接口的全称是"Serial Peripheral Interface",意为串行外围接口,是Motorola首先在其MC6 ...
- win7 奇怪的temp用户
在C:\Users\TEMP 有个temp用户,win+r打开的也是 C:\Users\TEMP>,而不是C:\User\Administrator. 以下文章转自: http://hi.bai ...
- bzoj1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of wh ...
- Jquery时间快捷控件(Jtime)配置v1.1
1.插件代码 /** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * @date 2013-0 ...
- popToViewController用法
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIn ...