Spring MVC中使用errors标签

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

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()方法判断

posted @ 2018-10-16 16:18  魔豆  阅读(2020)  评论(0编辑  收藏  举报