欢迎来到天天文库
浏览记录
ID:37712961
大小:36.50 KB
页数:8页
时间:2019-05-29
《SpSpringMVC数据验证》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、SpringMVC数据验证在这里我们采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证。首先我们要到http://hibernate.org/validator/下载需要的jar包,这里以4.3.1.Final作为演示,解压后把hibernate-validator-4.3.1.Final.jar、jboss-logging-3.1.0.jar、validation-api-1.0.0.GA.jar这三个包添加到项目中。配置
2、之前项目中的springservlet-config.xml文件,如下:复制代码3、viderClass"value="org.hibernate.validator.HibernateValidator"/>4、ort.FormattingConversionServiceFactoryBean"/>5、8"/>复制代码其中中的classpath:validatemessages为注解验证消息所在的文件,需要我们在resources文件夹下添加。在com.demo.web.controllers包中添加一个ValidateController.java内容如下:复制代码packagecom.demo6、.web.controllers;importjava.security.NoSuchAlgorithmException;importjavax.validation.Valid;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.validation.BindingResult;importorg.springframework.web.bin7、d.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importcom.demo.web.models.ValidateModel;@Controller@RequestMapping(value="/validate")publicclassValidateContr8、oller{@RequestMapping(value="/test",method={RequestMethod.GET})publicStringtest(Modelmodel){if(!model.containsAttribute("contentModel")){model.addAttribute("contentModel",newValidateModel());}re
3、viderClass"value="org.hibernate.validator.HibernateValidator"/>4、ort.FormattingConversionServiceFactoryBean"/>5、8"/>复制代码其中中的classpath:validatemessages为注解验证消息所在的文件,需要我们在resources文件夹下添加。在com.demo.web.controllers包中添加一个ValidateController.java内容如下:复制代码packagecom.demo6、.web.controllers;importjava.security.NoSuchAlgorithmException;importjavax.validation.Valid;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.validation.BindingResult;importorg.springframework.web.bin7、d.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importcom.demo.web.models.ValidateModel;@Controller@RequestMapping(value="/validate")publicclassValidateContr8、oller{@RequestMapping(value="/test",method={RequestMethod.GET})publicStringtest(Modelmodel){if(!model.containsAttribute("contentModel")){model.addAttribute("contentModel",newValidateModel());}re
4、ort.FormattingConversionServiceFactoryBean"/>5、8"/>复制代码其中中的classpath:validatemessages为注解验证消息所在的文件,需要我们在resources文件夹下添加。在com.demo.web.controllers包中添加一个ValidateController.java内容如下:复制代码packagecom.demo6、.web.controllers;importjava.security.NoSuchAlgorithmException;importjavax.validation.Valid;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.validation.BindingResult;importorg.springframework.web.bin7、d.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importcom.demo.web.models.ValidateModel;@Controller@RequestMapping(value="/validate")publicclassValidateContr8、oller{@RequestMapping(value="/test",method={RequestMethod.GET})publicStringtest(Modelmodel){if(!model.containsAttribute("contentModel")){model.addAttribute("contentModel",newValidateModel());}re
5、8"/>复制代码其中中的classpath:validatemessages为注解验证消息所在的文件,需要我们在resources文件夹下添加。在com.demo.web.controllers包中添加一个ValidateController.java内容如下:复制代码packagecom.demo
6、.web.controllers;importjava.security.NoSuchAlgorithmException;importjavax.validation.Valid;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.validation.BindingResult;importorg.springframework.web.bin
7、d.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importcom.demo.web.models.ValidateModel;@Controller@RequestMapping(value="/validate")publicclassValidateContr
8、oller{@RequestMapping(value="/test",method={RequestMethod.GET})publicStringtest(Modelmodel){if(!model.containsAttribute("contentModel")){model.addAttribute("contentModel",newValidateModel());}re
此文档下载收益归作者所有