springmvc数据验证

来源:互联网 发布:linux ant是否安装 编辑:程序博客网 时间:2024/05/20 07:20
<!--数据验证-->     <dependency>     <groupId>org.hibernate</groupId>     <artifactId>hibernate-validator</artifactId>     <version>4.0.1.GA</version>     </dependency>       <!--jboss logging-->     <dependency>       <groupId>org.jboss.logging</groupId>       <artifactId>jboss-logging</artifactId>       <version>3.3.0.Final</version>>     </dependency>         <!--validation api-->     <dependency>       <groupId>javax.validation</groupId>       <artifactId>validation-api</artifactId>       <version>1.0.0.GA</version>     </dependency>         <!--slf4j api-->       <dependency>           <groupId>org.slf4j</groupId>           <artifactId>slf4j-api</artifactId>         <version>1.7.21</version>>       </dependency> 


public class UserInfo {    //必须是0到100之间    @Min(value = 0,message = "成绩最小值为{value}")    @Max(value = 100,message = "成绩最大值为{value}")    private Integer score;    //手机号码不为空        @NotEmpty(message = "手机号码不能为空")        @Pattern(regexp = "^1[3,4,5,6,7,8,9]\\d{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 Text {    @RequestMapping("/first")    public ModelAndView dofif(@Valid UserInfo info, BindingResult br){        ModelAndView mv=new ModelAndView();        mv.setViewName("/welcon.jsp");        int errorCount = br.getErrorCount();        if(errorCount>0){            FieldError score = br.getFieldError("score");            FieldError phone = br.getFieldError("phone");            FieldError name = br.getFieldError("name");            if (score!=null){              mv.addObject("scoremsg",score.getDefaultMessage());            }            if (phone!=null){                mv.addObject("phonemsg",phone.getDefaultMessage());            }            if (name!=null){                mv.addObject("namemsg",name.getDefaultMessage());            }          mv.setViewName("/valldator.jsp");        }        return mv;    }}


<?xml version="1.0" encoding="UTF-8"?><beans  xmlns="http://www.springframework.org/schema/beans"        xmlns:aop="http://www.springframework.org/schema/aop"        xmlns:tx="http://www.springframework.org/schema/tx"        xmlns:context="http://www.springframework.org/schema/context"        xmlns:p="http://www.springframework.org/schema/p"        xmlns:mvc="http://www.springframework.org/schema/mvc"        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/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <!--配置 包 扫描器-->    <context:component-scan base-package="cn.hello.Valldator"/>    <bean id="myValltor" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>    </bean>   <mvc:annotation-driven validator="myValltor"></mvc:annotation-driven></beans>


<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>   欢迎你</body>