Spring MVC验证用于限制用户提供的输入。
为了验证用户的输入,Spring 4或更高版本支持并使用Bean验证API。
它可以同时验证服务器端 和 客户端应用程序。
Bean验证API
Bean验证API是一种Java规范,可用于应用通过注释对对象模型的约束。在这里,我们可以验证长度,数字,正则表达式等。除此之外,我们还可以提供自定义验证。
由于Bean验证API只是一个规范,因此需要实现。因此,为此,它使用了Hibernate Validator。 Hibernate Validator是完全兼容的JSR-303/309实现,允许表达和验证应用程序约束。
验证注释
@NotNull 它确定该值不能为空。
@Min 确定该数字必须等于或大于指定的值。
@Max 确定该数字必须等于或小于指定的值。
@Size 确定大小必须等于指定的值。
@Pattern 它确定序列遵循指定的正则表达式。
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 com.nhooo; import javax.validation.constraints.Size; public class Employee { private String name; @Size(min=1,message="required") private String pass; 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; } }
3、创建控制器类
在控制器类中:
@有效注释将验证规则应用于所提供的对象。BindingResult 接口包含验证结果。
package com.nhooo; 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="com.nhooo" /> <!--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> <input type="submit" value="submit"> </form:form> </body> </html>
final.jsp
<html> <body> Username: ${emp.name} <br><br> Password: ${emp.pass} </body> </html>
![](/i/l/?n=23&i=blog/1320403/202301/1320403-20230118212907403-1996518372.png)
标签:String,验证,Spring,springframework,MVC,import,org,public From: https://www.cnblogs.com/cnetsa/p/17060625.html