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 ...
随机推荐
- 提高MySQL数据库查询效率的几个技巧(转载)
[size=5][color=Red]提高MySQL数据库查询效率的几个技巧(转)[/color][/size] MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我 ...
- nginx+mysql+php
根据生产环境安装操作系统(centos 6.0 64位系统),安装完成后,使用Xshell通过ssh协议连接服务器.ssh 用户名@IP+回车+输入密码后登录系统.#mkdir -p /home/to ...
- mvn打包发布
一:打包 cmd进入工作目录运行命令 1: mvn clean 2: mvn install 3: mvn clean compile 4: mvn package -DiskipTest ...
- Mysql基本类型(字符串类型)——mysql之二
转自: http://www.cnblogs.com/doit8791/archive/2012/05/28/2522556.html 1.varchar类型的变化 MySQL 数据库的varchar ...
- 前端MVVM学习之KnockOut(二)
现在开始学习Knockout并且做个简单的例子. Knockout是建立在以下三个核心功能之上的: 1.Observables and dependency tracking(属性监控与依赖跟踪) 2 ...
- LeetCode_Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 在非MFC的win 32程序里面能够使用CString类
论坛有会员用到了.,今天给大家说说CSring如何在非mfc下的调用第一:先要包含 #include "afx.h" 包含之后会报windows.h重复定义我们需要把这个头文件包含 ...
- printf "%.*s" 分类: 小细节 2015-07-04 14:36 2人阅读 评论(0) 收藏
ref : http://www.cnblogs.com/yuaqua/archive/2011/10/21/2219856.html 小数点.后"*"表示输出位数,具体的数据来自 ...
- 改变UITableViewCell按下去的颜色
UIView *bgColorView = [[UIView alloc] init]; [bgColorView setBackgroundColor:[UIColor redColor]]; [c ...
- hdu2095 像水题的不错题 异或运算
异或运算的基础有点忘记了 先介绍一下..2个数异或 就是对于每一个二进制位进行位运算 具有2个特殊的性质 1.一个数异或本身恒等于0,如5^5恒等于0: 2.一个数异或0恒等于本身,如5^0恒等于5. ...