SpringMVC数据验证

来源:互联网 发布:比特币交易软件 编辑:程序博客网 时间:2024/05/20 20:23
实体:
public class UserInfo {    @Min(value=0,message="成绩最小值为{value}")    @Max(value=100,message = "成绩最大值为{value}")    private Integer score;    @NotEmpty(message = "手机号码不能为空")    @Pattern(regexp="1[3456789]\\{9}$",message = "手机号码不正确")    private String phone;    @NotEmpty(message = "用户名不能为空")    @Size(min=6,message = "名称至少 6个字符")    private String name;    public Integer getScore() {        return score;    }    public void setScore(Integer score) {        this.score = score;    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}


@Controllerpublic class First0830 {    @RequestMapping("/first")    public ModelAndView doFirst(@Valid UserInfo info,BindingResult br){        ModelAndView mv = new ModelAndView();        mv.setViewName("/Welcome.jsp");        int errorCount = br.getErrorCount();        if(errorCount>0){            FieldError score = br.getFieldError("score");            FieldError name = br.getFieldError("name");            FieldError phone = br.getFieldError("phone");            if(score!=null){                mv.addObject("scoremsg",score.getDefaultMessage());            }            if(name!=null){                mv.addObject("namemsg",name.getDefaultMessage());            }            if(phone!=null){                mv.addObject("namemsg",name.getDefaultMessage());            }            mv.setViewName("/first.jsp");        }        return mv;    }}


<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd     http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">    <context:component-scan base-package="cn.bdqn.controller0830"></context:component-scan>    <bean id="myValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>    </bean>    <mvc:annotation-driven validator="myValidator"></mvc:annotation-driven></beans>


<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>页面</title></head><body><h1>数据校验</h1><form action="/first" method="post">    成绩:<input name="score" /> <span>${scoremsg }</span><br/><br/>    姓名:<input name="name"/><span>${namemsg }</span><br/><br/>    电话:<input name="phone"/><span>${phonemsg }</span><br/><br/>    <input type="submit" value="注册"/></form></body></html>




原创粉丝点击