SpringMVC数据验证

本文介绍了一个使用Spring MVC框架实现的表单验证示例。通过定义一个包含注解验证的UserInfo类来确保用户输入的数据符合预期的格式和范围要求。文章展示了如何通过Spring MVC配置全局的验证器,并在控制器中处理验证失败的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实体:
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;
    }
}


@Controller
public 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>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值