数据验证分为客户端验证和服务器验证,
创建自定义的Spring验证器需要实现org.springframework.validation.Validator接口,该接口有两个方法
实现的数据输入页面效果图
数据显示页面
需要额外引入<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
addGoods.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 prefix="form" uri="http://www.springframework.org/tags/form" %>
Insert title here
modelAttribute:表示的是绑定的是哪一个Model,当指定了对应的Model后就可以在form标签内部其它表单标签上通过为path指定Model属性的名称来绑定Model中的数据了,method属性指定form的提交方式如GET、POST等
创建pojo实体类
public class Goods {
private String gname;
private String gdescription;
private double gprice;
//日期格式化
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date gdate;
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getGdescription() {
return gdescription;
}
public void setGdescription(String gdescription) {
this.gdescription = gdescription;
}
public double getGprice() {
return gprice;
}
public void setGprice(double gprice) {
this.gprice = gprice;
}
public Date getGdate() {
return gdate;
}
public void setGdate(Date gdate) {
this.gdate = gdate;
}
@Override
public String toString() {
return "Goods [gname=" + gname + ", gdescription=" + gdescription + ", gprice=" + gprice + ", gdate=" + gdate
+ "]";
}
}
编写验证器类
主要是实现Validator接口,实现方法,使用了类似于goods.gname.required这样的错误消息属性文件。
@Component
public class GoodsValidator implements Validator{
@Override
public boolean supports(Class> kclass) {
//要验证的model,返回值为false则不验证
return Goods.class.isAssignableFrom(kclass);
}
@Override
public void validate(Object object, Errors errors) {
Goods goods = (Goods)object;//要验证的对象
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");
}
}
}
配置错误消息属性文件,因为properties对于中文会自动编码
中文依次是:
请输入商品名称--请输入商品详情--价格为0-100--创建日期不能再系统日期之后
属性文件如下
goods.gname.required=\u8BF7\u8F93\u5165\u5546\u54C1\u540D\u79F0
goods.gdescription.required=\u8BF7\u8F93\u5165\u5546\u54C1\u8BE6\u60C5
gprice.invalid=\u4EF7\u683C\u4E3A0-100
gdate.invalid=\u521B\u5EFA\u65E5\u671F\u4E0D\u80FD\u518D\u7CFB\u7EDF\u65E5\u671F\u4E4B\u540E
Service层和ServiceImpl层代码
public interface GoodsService {
boolean save(Goods g);
ArrayList getGoods();
}
//serviceImpl层代码
@Service
public class GoodsServiceImpl implements GoodsService{
//使用静态集合变量goods模拟数据库
private static ArrayList goods = new ArrayList();
@Override
public boolean save(Goods g) {
goods.add(g);
return true;
}
@Override
public ArrayList getGoods() {
return goods;
}
}
Controller层代码
主要实现商品未填写跳转到添加商品页面和商品填写错误的验证判断
@Controller
@RequestMapping("/goods")
public class GoodsController {
//日志
private static final Log logger = LogFactory.getLog(GoodsController.class);
@Autowired
private GoodsService goodsService;
//相当于 GoodsValidator validator = new GoodsValidator()
@Resource
private Validator validator;
@RequestMapping("/input")
public String input(Model model) {
//如果model中没有goods属性,addGoods.jsp就会抛出异常
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);
logger.info("添加成功");
model.addAttribute("goodList", goodsService.getGoods());
return "goodList";
}
}
springmvc-servlet.xml文件配置
数据显示页面
商品名称
商品详情
商品价格
创建日期
${goods.gname }
${goods.gdescription }
${goods.gprice }
${goods.gdate }
web.xml文件配置
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring-config/springmvc-servlet.xml
1
springmvc
/
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encoding
/*
创建验证器:
定义一个验证器类,实现Spring验证器接口,通过接口中的方法实现来完成验证器功能的实现,从而完成验证器的创建。