Spring MVC数字验证

在Spring MVC验证中,我们可以验证用户输入是否在数字范围内。以下注解用于实现数字验证:

  • @Min注解 - 必须使用@Min注解传递一个整数值。用户输入的值必须等于或大于这个值。
  • @Max注解 - 必须使用@Max注解传递一个整数值。用户输入的值必须等于或小于这个值。

Spring MVC数字验证示例

1. 在pom.xml文件中添加依赖。

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-webmvc</artifactId>  
    <version>5.1.1.RELEASE</version>  
</dependency>  
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->  
<dependency>  
    <groupId>org.apache.tomcat</groupId>  
    <artifactId>tomcat-jasper</artifactId>  
    <version>9.0.12</version>  
</dependency>  
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
<dependency>    
    <groupId>javax.servlet</groupId>    
    <artifactId>servlet-api</artifactId>    
    <version>3.0-alpha-1</version>    
</dependency>  
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->  
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>jstl</artifactId>  
    <version>1.2</version>  
</dependency>  
 <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->  
<dependency>  
    <groupId>org.hibernate.validator</groupId>  
    <artifactId>hibernate-validator</artifactId>  
    <version>6.0.13.Final</version>  
</dependency>  

2. 创建Bean类

Employee.java

package cn.javatiku;  
  
import javax.validation.constraints.Max;  
import javax.validation.constraints.Min;  
import javax.validation.constraints.Size;  
  
public class Employee {  
  
    private String name;  
    @Size(min=1,message="required")  
    private String pass;  
      
    @Min(value=18, message="must be equal or greater than 18")  
    @Max(value=45, message="must be equal or less than 45")  
    private int age;  
      
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getPass() {  
        return pass;  
    }  
    public void setPass(String pass) {  
        this.pass = pass;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
      
}  

3. 创建控制器类

EmployeeController.java

 package cn.javatiku;  
  
import javax.validation.Valid;  
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;  
  
@Controller  
public class EmployeeController {  
  
    @RequestMapping("/hello")  
    public String display(Model m)  
    {  
        m.addAttribute("emp", new Employee());  
        return "viewpage";  
    }  
    @RequestMapping("/helloagain")  
    public String submitForm( @Valid @ModelAttribute("emp") Employee e, BindingResult br)  
    {  
        if(br.hasErrors())  
        {  
            return "viewpage";  
        }  
        else  
        {  
        return "final";  
        }  
    }  
}  

4. 在web.xml文件中定义控制器条目

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  <display-name>SpringMVC</display-name>  
   <servlet>    
    <servlet-name>spring</servlet-name>    
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
    <load-on-startup>1</load-on-startup>      
</servlet>    
<servlet-mapping>    
    <servlet-name>spring</servlet-name>    
    <url-pattern>/</url-pattern>    
</servlet-mapping>    
</web-app>  

5. 在xml文件中定义Bean

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    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/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
    <!-- Provide support for component scanning -->  
    <context:component-scan base-package="cn.javatiku" />  
    <!--Provide support for conversion, formatting and validation -->  
    <mvc:annotation-driven/>  
    <!-- Define Spring MVC view resolver -->  
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/"></property>  
        <property name="suffix" value=".jsp"></property>       
     </bean>  
</beans>  

6. 创建所请求的页面

index.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
<html>  
<body>  
<a href="hello">Click here...</a>  
</body>  
</html>  

7. 创建其他视图组件

viewpage.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>  
<html>  
<head>  
<style>  
.error{color:red}  
</style>  
</head>  
<body>  
<form:form action="helloagain" modelAttribute="emp">  
Username: <form:input path="name"/> <br><br>  
Password: <form:password path="pass"/>    
<form:errors path="pass" cssClass="error"/><br><br>  
Age: <form:input path="age"/>   
<form:errors path="age" cssClass="error"/><br><br>  
<input type="submit" value="submit">  
</form:form>  
</body>  
</html>  

final.jsp

<html>  
<body>  
Username: ${param.name} <br>  
Password: ${param.pass} <br>  
Age: ${param.age } <br>  
</body>  
</html>  

输出:

spring-mvc-number-validation-output1.png

spring-mvc-number-validation-output2.png

spring-mvc-number-validation-output3.png

spring-mvc-number-validation-output4.png

spring-mvc-number-validation-output5.png

spring-mvc-number-validation-output5.png

spring-mvc-number-validation-output6.png

标签: spring, Spring教程, Spring技术, Spring语言学习, Spring学习教程, Spring下载, Spring框架, Spring框架入门, Spring框架教程, Spring框架高级教程, Spring面试题, Spring笔试题, Spring编程思想