@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的更多相关文章

  1. Spring中的DataBinding(一)

    DataBinding在Spring中应用. 第一点:使用ModelAttribute指定带绑定的数据内容 很重要的属性:@ModelAttribute([value=""])可以 ...

  2. spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)

    在上篇博客中分享了InstantiationAwareBeanPostProcessor接口中的四个方法,分别对其进行了详细的介绍,在文末留下了一个问题,那就是postProcessPropertie ...

  3. spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...

  4. spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...

  5. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  6. spring boot高性能实现二维码扫码登录(中)——Redis版

    前言 本打算用CountDownLatch来实现,但有个问题我没有考虑,就是当用户APP没有扫二维码的时候,线程会阻塞5分钟,这反而造成性能的下降.好吧,现在回归传统方式:前端ajax每隔1秒或2秒发 ...

  7. Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用

    Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...

  8. Spring学习(二)——Spring中的AOP的初步理解

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  9. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

随机推荐

  1. debian安装缺少网卡驱动rtl_nic/rtl8168e-2.fw和bcm43xx-0.fw

    最近拿了公司一台旧笔记本来装debian玩,刻好盘之后安装,发现网卡驱动都无法装上. 这是因为这些网卡驱动不是开源的,顽固的debian便将他们从驱动列表中移除了... 我从debian官方找到了这两 ...

  2. [C++程序设计]函数的递归调用

    在调用一个函数的过程中又出现直接或间接地调用 该函数本身,称为函数的递归(recursive)调用. 包含递归调用的函数称为递归函数. 在实现递归时,在时间和空间上的开销比较大 求n! #includ ...

  3. [转]MySQL 5.6 全局事务 ID(GTID)实现原理(一)

    原文作者:淘长源 原文连接:http://qing.blog.sina.com.cn/1757661907/68c3cad333002qhe.html 转载注明以上信息   MySQL 5.6 的新特 ...

  4. H5本地存储

    在HTML5中可以把数据长期存储在客户端,使用的对象就是localStorage. localStorage常用方法有setItem.getItem.removeItem.clear. 下面是一个存储 ...

  5. python安装第三方包的两种方式

    最近研究QQ空间.微博的(爬虫)模拟登录,发现都涉及RSA算法.于是需要下一个RSA包(第三方包).折腾了很久,主要是感觉网上很多文章对具体要在哪里操作写得不清楚.这里做个总结,以免自己哪天又忘了. ...

  6. 典型的DIV CSS三行二列居中高度自适应布局

    如何使整个页面内容居中,如何使高度适应内容自动伸缩.这是学习CSS布局最常见的问题.下面就给出一个实际的例子,并详细解释.(本文的经验和是蓝色理想论坛xpoint.guoshuang共同讨论得出的.) ...

  7. typedef函数指针那些事

    首先来介绍下函数指针: 函数指针是指向函数的指针变量,即本质是一个指针变量. int (*f) (int x); /* 声明一个函数指针 */ f=func; /* 将func函数的首地址赋给指针f ...

  8. 信号量 <第六篇>

    一.ManualResetEvent 该对象有两种信号量状态True和False.构造函数设置初始状态. WaitOne:该方法用于阻塞线程,默认是无限期的阻塞,支持超时阻塞,如果超时就放弃阻塞,这样 ...

  9. shell脚本语法基础汇总

    shell脚本语法基础汇总 将命令的输出读入一个变量中,可以将它放入双引号中,即可保留空格和换行符(\n) out=$(cat text.txt) 输出1 2 3 out="$(cat te ...

  10. 携程SQL面试题忘大牛解答解决思路

    讨论地址:http://bbs.csdn.net/topics/380208742