@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. 提高MySQL数据库查询效率的几个技巧(转载)

    [size=5][color=Red]提高MySQL数据库查询效率的几个技巧(转)[/color][/size]      MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我 ...

  2. MySQL调试

    http://dev.mysql.com/doc/refman/5.5/en/signal.html#signal-condition-information-items http://www.dev ...

  3. DOSUSB 2.0 免费版的限制原理

    两年前,我在写USB的文章时,多次提到了DOSUSB这个东东,这两年也没有关注这方面的变化,最近,有机会重新进入DOSUSB的官方网站(www.dosusb.net),欣喜地发现,这个网站不仅依然存在 ...

  4. OVERLAY代码重入

    OVERLAY代码重入问题:自己遇到的问题 编写的测试代码如下: #include <stdio.h> #define BYTE unsigned char #define BYTE un ...

  5. java时间比较

    public static void main(String[] args) throws Exception{//判断某个时间是否为当前时间 String dataStr="2014-07 ...

  6. css案例学习之按钮超链接

    效果 css实现 <html> <head> <title>按钮超链接</title> <style> a{ /* 统一设置所有样式 */ ...

  7. Word Search II 解答

    Question Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

  8. easy_install django==1.4.2_百度搜索

    easy_install django==1.4.2_百度搜索 安装指定版本的django

  9. 产生冠军(set,map,拓扑结构三种方法)

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. android scrollview 简单的使用

    以前写的Scrollview ,通常都是与Listview结合使用,不过因复杂可能新手不太懂,网上有许多文章,这里就不贴那个了DEMO了.  写了个简单的供大家参考:这样比较好理解(需要复杂的可以Q我 ...