Spring MVC验证器(15)

    技术2022-08-09  106

    Spring MVC验证器

    测试结果工程结构工程目录添加商品页addgoods.jsp控制器goodsController验证器GoodsValidator错误提示属性文件errorMessages

    测试结果

    工程结构

    工程目录

    添加商品页addgoods.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加商品</title> </head> <body> <form:form modelAttribute="goods" action="/goods/save" method="post"> <fieldset> <legend> 添加一件商品 </legend> <P> <label>商品名:</label> <form:input path="gname" /> </p> <p> <label>商品详情:</label> <form:input path="gdescription" /> </p> <P> <label>商品价格:</label> <form:input path="gprice" /> </p> <P> <label>创建日期:</label> <form:input path="gdate" /> (yyyy-MM-dd) </p> <p id="buttons"> <input id="reset" type="reset"> <input id="submit" type="submit" value="添加"> </p> </fieldset> <!-- 取出所有验证错误 --> <form:errors path="*" /> </form:form> </body> </html>

    控制器goodsController

    package control; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import pojo.Goods; import service.GoodsServiceImpl; import validator.GoodsValidator; @Controller @RequestMapping("/goods") public class GoodsController { @Autowired private GoodsServiceImpl goodsService; GoodsValidator validator = new GoodsValidator(); @RequestMapping("/input") public String input(Model model) { // 如果model中没有goods属性,addGoods.jsp会抛出异常 // 因为表单标签无法找到modelAttribute属性指定的form backing object model.addAttribute("goods", new Goods()); return "addgoods"; } @RequestMapping("/save") public String save(@ModelAttribute Goods goods, BindingResult result, Model model) { this.validator.validate(goods, result); // 添加验证 if (result.hasErrors()) { return "addgoods"; } goodsService.save(goods); model.addAttribute("goodsList", goodsService.getGoods()); return "showgoods"; } }

    验证器GoodsValidator

    package validator; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; import pojo.Goods; import java.util.Date; @Component public class GoodsValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return Goods.class.isAssignableFrom(clazz); } @Override public void validate(Object target, Errors errors) { Goods goods = (Goods)target; ValidationUtils.rejectIfEmpty(errors,"gname","goods.gname.required"); ValidationUtils.rejectIfEmpty(errors,"gdescription","goods.gdescription.required"); if(goods.getGprice()>100 || goods.getGprice()<0){ errors.rejectValue("gprice","gprice.invalid"); } Date goodsDate = goods.getGdate(); if(goodsDate != null && goodsDate.after(new Date())){ errors.rejectValue("gdate","gdate.invalid"); } } }

    错误提示属性文件errorMessages

    goods.gname.required=no name goods.gdescription.required=no description gprice.invalid=over 0~100 gdate.invalid=date invalid
    Processed: 0.019, SQL: 9