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 ...
随机推荐
- 用H5实现微信的四个界面
代码更新中
- Python之路第十二天,高级(5)-Python操作Mysql,SqlAlchemy
Mysql基础 一.安装 Windows: 1.下载 http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.31-winx64.zip 2.解压 ...
- python 连接操作数据库(一)
一.下面我们所说的就是连接mysql的应用: 1.其实在python中连接操作mysql的模块有多个,在这里我只给大家演示pymysql这一个模块(其实我是感觉它比较好用而已): pymysql是第三 ...
- Excel导出cs文件
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- ServerProperties
Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动.其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat).当然你也可以将项 ...
- 杭电1010(dfs + 奇偶剪枝)
题目: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked ...
- live555 源代码简单分析1:主程序
live555是使用十分广泛的开源流媒体服务器,之前也看过其他人写的live555的学习笔记,在这里自己简单总结下. live555源代码有以下几个明显的特点: 1.头文件是.hh后缀的,但没觉得和. ...
- PB C/S轉B/S ODBC方式連接數據庫
PB C/S轉B/S ODBC方式連接數據庫,DSN需要建為系統而不是使用者DSN,否則連不上數據庫.
- 删除一个表中的重复数据同时保留第一次插入那一条以及sql优化
业务:一个表中有很多数据(id为自增主键),在这些数据中有个别数据出现了重复的数据. 目标:需要把这些重复数据删除同时保留第一次插入的那一条数据,还要保持其它的数据不受影响. 解题过程: 第一步:查出 ...
- 力挺8天入门wpf【转载】
8天入门wpf—— 第八天 最后的补充 摘要: 从这一篇往前看,其实wpf中还有很多东西没有讲到,不过我的原则还是将比较常用的知识点过一遍,如果大家熟悉了这些知识,基本功也就打的差不多了,后续可以等待 ...