SPRINGMVC Background Data Check

Source: Internet
Author: User

Step ①: Introducing Dependency

<!--data checksum        <dependency>            <groupId>org.hibernate</groupId>            <artifactid >hibernate-validator</artifactId>            <version>4.3.1.Final</version>        </dependency >        <!--validation api-->        <dependency>            <groupId>javax.validation</groupId>            <artifactId>validation-api</artifactId>            <version>1.0.0.GA</version>        </dependency>

  

Step ②: Configure Spring.xml

<!--: Parameter Method name Resolver--   <bean id= "Methodnameresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver ">       <property name=" prefix "value="/"/ >       <property name= "suffix" value= ". jsp"/>   </bean>    <!--Scan all of the labeled classes under the package--    < Context:component-scan base-package= "Cn.happy.day15Validator"/>    <!--configuration Validator--    <bean id= " Myvalidator "class=" Org.springframework.validation.beanvalidation.LocalValidatorFactoryBean ">        < Property Name= "Providerclass" value= "Org.hibernate.validator.HibernateValidator"/>    </bean>    <!--binding Validator--    <mvc:annotation-driven validator= "Myvalidator"/>

  

Step ③: Entity class annotations

Package Cn.happy.day15validator;import Org.hibernate.validator.constraints.notempty;import Javax.validation.constraints.max;import Javax.validation.constraints.min;import Javax.validation.constraints.pattern;import javax.validation.constraints.size;/** * Created by Administrator on 2018/ 3/31. */public class UserInfo {/** * min: Set minimum value * Max: Set maximum * message: Print info *//Specify Age between 18-120 @Min (valu    E = 18,message = "Age cannot be less than 18 years old") @Max (value = 120,message = "Age cannot be greater than 100 years old") private Integer-age; /** * Three ways to verify * @NotEmpty for collection * @NotNull for base type * @NotBlank for reference type * @Size set size *//non-empty test    Certificate @NotEmpty (message = "User name cannot be null") @Size (max = 20,min = 6,message = "User name cannot be greater than 20 characters or less than 6 characters") Private String name; /** * @Pattern REHEXP matches the regular */@NotEmpty (message = "Mailbox cannot be Empty") @Pattern (regexp = "^\\[email protected]\\w    +/.\\w+$ ", message =" Bad mailbox format ") Private String email;   @Pattern (regexp = "^1[35789]\\d{9}$", message = "Phone number format error") Private String phone;    Public String Getphone () {return phone;    } public void Setphone (String phone) {this.phone = phone;    } public String Getemail () {return email;    } public void Setemail (String email) {this.email = email;    } public Integer Getage () {return age;    public void Setage (Integer age) {this.age = age;    } public String GetName () {return name;    } public void SetName (String name) {this.name = name; }}

  

Step ④: Configure the processor

Import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.validation.bindingresult;import Org.springframework.validation.fielderror;import Org.springframework.web.bind.annotation.requestmapping;import Javax.validation.valid;import Java.text.simpledateformat;import java.util.date;/** * Created by Administrator on 2018/3/30. */@Controllerpublic class Firstcontroller {@RequestMapping ("/first")/** * @Valid Validation Object * Bindingresult Binding Fixed results * Model View Object */Public String Dofirst (@Valid UserInfo UserInfo, Bindingresult BR, Model mv) throws Excep tion {//Determine number of errors in BR if (Br.geterrorcount () >0) {//prove at least one data error//Get information Fielderror name            = Br.getfielderror ("name");            Fielderror age = Br.getfielderror ("Age");            Fielderror phone = br.getfielderror ("Phone");            Fielderror email = br.getfielderror ("email");              Get error message if (name!=null) {  String namemsg = Name.getdefaultmessage ();            Return the error message to page Mv.addattribute ("Namemsg", namemsg);                } if (age!=null) {String agemsg = Age.getdefaultmessage ();            Mv.addattribute ("Agemsg", agemsg);                } if (phone!=null) {String phonemsg = Phone.getdefaultmessage ();            Mv.addattribute ("Phonemsg", phonemsg);                } if (email!=null) {String emailmsg = Email.getdefaultmessage ();            Mv.addattribute ("Emailmsg", emailmsg);        }//Return request View return "Validator";    } return "Dosecond"; }}

  Configuration Complete!

SPRINGMVC Background Data Check

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.