测试结果
工程结构
工程目录
添加商品页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.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