先创建一个实体类,后续的验证都基于这个实体类:

public class Goods {

    private String goodsName;
private String city;
private int amount; public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
} }

表单页面的controller方法:

    @RequestMapping("/test1")
public String test1(Model model) {
Goods goods = new Goods();
goods.setGoodsName("联想电脑");
goods.setCity("北京");
goods.setAmount(10);
model.addAttribute("goods",goods); return "/test1";
}

表单jsp页面

test1.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test1</title>
</head>
<body>
<form:form modelAttribute="goods" method="post" action="test2">
<form:input path="goodsName" />
<form:errors path="goodsName"></form:errors>
<br />
<c:set var="citys" value="北京,上海,广州" />
<c:set var="cityList" value="${fn:split(citys,',') }" />
<form:select path="city">
<form:option value="">请选择</form:option>
<form:options items="${cityList }" />
</form:select>
<form:errors path="city"></form:errors>
<br />
<form:button>提交</form:button>
</form:form>
</body>
</html>

提交表单的controller:

    @InitBinder
public void initBinder(DataBinder binder) {
binder.setValidator(new Validator() { @Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "goodsName", null, "商品名称不能为空");
ValidationUtils.rejectIfEmpty(errors, "city", null, "城市不能为空"); Goods goods = (Goods) target;
if(goods.getGoodsName().length()<4) {
errors.rejectValue("goodsName", null, "商品名称不能少于4个字符");
}
} @Override
public boolean supports(Class<?> clazz) { return Goods.class.equals(clazz);
}
});
} @RequestMapping(value="/test2",method=RequestMethod.POST)
public String test2(@Validated Goods goods,Errors errors,BindingResult result) {
if(errors.hasErrors()) {
return "/test1";
} else {
return "/test2";
}
}

通过@InitBinder注解的方法,会在test2方法前执行,如果检查出了错误,会在test2方法里通过errors.hasErrors()方法判断

Spring MVC中使用errors标签的更多相关文章

  1. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  2. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

  3. Spring MVC中处理静态资源的多种方法

    处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中 ...

  4. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  5. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  6. Http请求中Content-Type讲解以及在Spring MVC中的应用【转】

    完全引用自: http://blog.csdn.net/blueheart20/article/details/45174399#t1   此文讲得很清晰,赞! 引言: 在Http请求中,我们每天都在 ...

  7. Http请求中Content-Type和Accept讲解以及在Spring MVC中的应用

    在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值,以及在 ...

  8. [转]Http请求中Content-Type讲解以及在Spring MVC中的应用

    本文转自:http://blog.csdn.net/blueheart20/article/details/45174399 引言: 在Http请求中,我们每天都在使用Content-type来指定不 ...

  9. Spring MVC 中的基于注解的 Controller(转载)

           终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...

随机推荐

  1. Promise,Generator(生成器),async(异步)函数

    Promise 是什么 Promise是异步编程的一种解决方案.Promise对象表示了异步操作的最终状态(完成或失败)和返回的结果. 其实我们在jQuery的ajax中已经见识了部分Promise的 ...

  2. MyBatis-Generator最佳实践

    引用地址:http://arccode.net/2015/02/07/MyBatis-Generator%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/ 最近使用MyBati ...

  3. 缩放到被选择的部分: ICommand Cmd = new ControlsZoomToSelectedCommandClass();

    AddItem("esriControls.ControlsZoomToSelectedCommand"); //ICommand Cmd = new ControlsZoomTo ...

  4. Ubuntu14.04LTS下使用eclipse搭建Cocos2d-x的Android环境

    from://http://www.58player.com/blog-2534-94136.html 最近想玩玩游戏制作,于是选择了目前流行的游戏引擎Cocos2d-x,这个东西虽然有Android ...

  5. Step Detector and Step Counter Sensors on Android

    Step Detector and Step Counter Sensors on Android 时间 2014-03-31 11:56:00 Tech Droid 原文  http://techd ...

  6. SharePoint 2013 安装.NET Framework 3.5 报错

    环境描述 操作系统:Windows Server 2012 R2 Datacenter版本 安装报错 中途接手安装SharePoint Server 2013 with sp1,配置向导报错如下: A ...

  7. git 拉取远程指定分支 pull本地不存在的分支

    默认,git项目只有一个分支,就是master,我们当然可以在本地创建多个分支,并推送到远程git管理平台上,或者将远程git管理平台上的其他分支拉取到自己电脑上. 一.查看本地已有的分支 进入到项目 ...

  8. 部署Percona XtraDB Cluster高可用和多Master集群

    http://www.it165.net/admin/html/201401/2306.html http://www.oschina.net/p/percona-xtradb-cluster/ ht ...

  9. ios开发第三方库--cocoapods安装

    1. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&quo ...

  10. 使用DebugView小工具调试已部署的.net程序 (转)

    DebugView for Windows能够捕捉Debug输出的信息在本地的操作系统上.如何你需要调试程序有网络访问推荐使用Wireshark和监听HTTP的工具Fiddler. 下载下来是一个ZI ...