English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Spring MVC验证使我们可以按特定顺序(即正则表达式)验证用户输入。 @Pattern Spring MVC验证使我们可以按特定顺序(即正则表达式)验证用户输入。 @Pattern 批注用于实现正则表达式验证。在这里,我们可以为
Spring MVC正则表达式验证示例
<!-- <artifactId>jstl<//./https:/、将依赖项添加到pom.xml文件。/spring-pom.xml --> hibernate org.springframework/<dependency> webmvc-<groupId>org.springframework</<artifactId>validator< <version>5<groupId>javax.servlet<1<groupId>javax.servlet<1<artifactId>spring/version> </dependency> <!-- <artifactId>jstl<//./https:/webmvc</.RELEASE<-org.apache.tomcat --> hibernate tomcat/<dependency> jasper-<groupId>org.apache.tomcat</<artifactId>validator< <version>9.0.12</version> </dependency> <!-- <artifactId>jstl<//./https:/.0/.0-<artifactId>tomcat --> hibernate javax.servlet/<dependency> jasper<-api/<artifactId>validator< <version>3<artifactId>servlet-api<-1</version> </dependency> <!-- <artifactId>jstl<//./https:/.0/alpha --> hibernate javax.servlet/<dependency> jstl/<artifactId>validator< <version>1<groupId>javax.servlet<2</version> </dependency> <!-- <artifactId>jstl<//./https:/mvnrepository.com/artifact-org.hibernate.validator --> hibernate validator/<dependency> <groupId>org.hibernate.validator<-<artifactId>hibernate/<artifactId>validator< <version>6.0.13.Final</version> </dependency>
Employee.java
package com.w3codebox; import javax.validation.constraints.Pattern; public class Employee { private String name; @Pattern(regexp="^[a-zA-Z0-9]{3}"message="length must be 3}) 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; } }
package com.w3codebox; 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"; } } }
web.xml
<?xml version="1.0" encodage="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"> <affichage-nom>SpringMVC</affichage-nom> <servlet> <servlet-nom>spring</servlet-nom> <servlet-classe>org.springframework.web.servlet.DispatcherServlet</servlet-classe> <chargement-sur-démarrage>1</chargement-sur-démarrage> </servlet> <servlet-cartographie> <servlet-nom>spring</servlet-nom> <url-motif>/</url-motif> </servlet-cartographie> </web-app>
spring-servlet.xml
<?xml version="1.0" encodage="UTF-8"?> <beans xmlns="http://www.springframework.org/schéma/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schéma/context" xmlns:mvc="http://www.springframework.org/schéma/mvc" xsi:schemaLocation=" http://www.springframework.org/schéma/beans http://www.springframework.org/schéma/beans/spring-beans.xsd http://www.springframework.org/schéma/context http://www.springframework.org/schéma/context/spring-context.xsd http://www.springframework.org/schéma/mvc http://www.springframework.org/schéma/mvc/spring-mvc.xsd"> <!-- Fournir un soutien pour le balayage des composants --> <context:component-scanner-paquet="com.w3codebox" /> <!--Fournir un soutien pour la conversion, le formatage et la validation --> <mvc:annotation-drivé/> <!-- Définir le résolveur de vue Spring MVC --> <bean id="resolveurDeVue" classe="org.springframework.web.servlet.view.InternalResourceViewResolver"> <propriété nom="prefix" valeur="/WEB-INF/jsp/></propriété> <propriété nom="suffix" valeur=".jsp"></propriété> </haricot> </haricots>
index.jsp
index.jsp <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <body> <a href="hello">Cliquez ici...</a> </body> </html>
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"> Nom d'utilisateur: <form:input path="name"/> <br><br> Mot de passe(*): <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> Nom d'utilisateur: ${emp.name} <br><br> Mot de passe: ${emp.pass} </body> </html>
Sortie: