Validation configuration for Springmvc4

Source: Internet
Author: User

There are two configurations, one for inheriting a validation class and one for annotations.

At the same time, the annotation can be subdivided, one is to hard code the prompt information into the codes, this configuration is simple, another slightly troublesome point, but you can write the error message to the configuration file.

First the POM

<!--annotations Specification Javax and verification--><dependency><groupid>javax.validation</groupid><artifactid> Validation-api</artifactid><version>1.1.0.final</version></dependency><dependency ><groupId>javax.validation</groupId><artifactId>validation-api</artifactId>< version>1.1.0.final</version></dependency><dependency><groupid>org.hibernate</ Groupid><artifactid>hibernate-validator</artifactid><version>5.2.2.final</version> </dependency>

Next page

<%@ page language= "java"  contenttype= "Text/html; charset=utf-8"      pageencoding= "UTF-8"%><% @taglib  uri= "Http://www.springframework.org/tags/form"  prefix= "SF"  %> <%string path = request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd "> 

Then controller

/** * <p>description: </p> * @author SCC * @since created: November 3, 2015 morning 10:18:15 */@Controllerpublic class Homecon Troller {@Value ("#{testdao}") Private Itestdao Itestdao, @RequestMapping ("Home") public String Test1 (model model) { Model.addattribute ("Testvo", New Testvo ()); return "index";} @RequestMapping ("/add") public String test2 (@Valid @ModelAttribute ("Testvo") Testvotestvo,bindingresult result) {if ( Result.haserrors ()) {return "index";} else {itestdao.add (TESTVO); return "Index";}}}

and a testvo.

/** * <p>description: </p> * @author SCC * @since created: November 3, 2015 morning 11:15:31 */public class Testvo {@Size (min= 3,max=5,message= "{testvo.name}") private String name; @NotNull (message= "{testvo.age}") Private Integer age;/* Omit Setter and getter */}

Note that the message property, if you want to hard-coded prompt information, then the configuration is complete, the message is written in the message can be, if you use another, continue to look down, in the SPRINGMVC configuration file

 <!--&NBSP;SPRING&NBSP;MVC validation begins   If you do not use this configuration, you need to hardcode the error information into your code, which is not easy to modify-->    <!--   Support for default annotation mappings  -->      <mvc:annotation-driven validator= " Validator " conversion-service=" Conversion-service " />         <bean id= "Validator"  class= " Org.springframework.validation.beanvalidation.LocalValidatorFactoryBean ">         <property name= "Providerclass"   value= " Org.hibernate.validator.HibernateValidator "/>        <!-- Do not set default to  ValidationMessages.properties -->        under Classpath  <property name= "Validationmessagesource"  ref= "Validatemessagesource"/>     </bean>    <bean id= "Conversion-service"  class= " Org.springframework.format.support.Formattingconversionservicefactorybean " />    <bean id=" Validatemessagesource " class=" Org.springframework.context.support.ReloadableResourceBundleMessageSource " >          <property name= "basename"  value= " Classpath:validatemessages "/>          <property  Name= "Fileencodings"  value= "Utf-8"/>          < Property name= "Cacheseconds"  value= "/>      </bean> "    <!--&NBSP;SPRING&NBSP;MVC Verification End  -->

At the same time in classpath under the establishment of Validatemessages.properties, code must be Utf-8

If you have Chinese, write it in another document, then paste it in, and automatically turn it into Unicode encoding. In the configuration file, you can also use {} to get some simple attributes, such as Min, Max,

Testvo.name={min}\u54c8{max}\u54c8hahatestvo.age=heihei

To see more detailed documentation, click Http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/validator-usingvalidator.html.

Validation configuration for Springmvc4

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.