Spring mvc:hibernate Validator (field cannot be empty, in 1-150 itself)

Source: Internet
Author: User

Spring mvc:hibernate Validator (field cannot be empty, in 1-150 itself)

Get ready:

Download Hibernate Validator Library-Hibernate Validator. Unziphibernate-validator-5.3.4.Final.jar

Create a new classes folder under/web-inf/

Access Address:

Http://localhost:8080/gugua3/student/index

Project: GUGUA3

Package Name: Springtest

This is used in the Hibernate-validator package, range range random number annotation, notempty not empty annotations

For example:

Import Org.hibernate.validator.constraints.notempty;import Org.hibernate.validator.constraints.range;public class Student {@NotEmptyprivate String   name; @Range (min=10, max=99) Private integer age;private integer ID;}

  

Configuration file: Web.xml,applicationcontext,springtest-servlet.xml

Xml

<web-app> <display-name>archetype Created Web application</display-name> <!--configuration file path-->< Context-param> <param-name>contextconfiglocation</param-name><param-value>/web-inf/ applicationcontext.xml</param-value></context-param><!--Character Filter-<filter> < Filter-name>encodingfilter</filter-name> <filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <param-name  >encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-patt Ern> </filter-mapping> <!--monitoring and forwarding--><listener><listener-class>      Org.springframework.web.context.contextloaderlistener</listener-class></listener><servlet> <servlet-name>springtest</sErvlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> &lt ;load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> springtest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-ap P>

  

Springtest-servlet.xml

<beansxmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC/HTTP Www.springframework.org/schema/mvc/spring-mvc.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context.xsd "><bean class=" Org.springframework.context.support.ResourceBundleMessageSource "id=" Messagesource "> <property name=" BaseName "value=" Messages "/></bean><bean class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf /jsp/"/> <property name=" suffix "value=". jsp "/> </bean></beans>

  

Applicationcontext.xml

<beansxmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC/HTTP Www.springframework.org/schema/mvc/spring-mvc.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context.xsd "><!--default: annotation map support--><mvc:annotation-driven/ ><!--Static resource configuration--><mvc:resources location= "/pages/**" mapping= "/pages/"/><!--automatically scan package name, controller-- ><context:component-scan base-package= "Springtest"/></beans>

  

Student.java

Package Springtest;import Org.hibernate.validator.constraints.notempty;import Org.hibernate.validator.constraints.range;public class Student {@NotEmptyprivate String   name; @Range (min=10, Max =99) Private integer age;private integer id;public String getName () {return name;} public void SetName (String name) {this.name = name;} Public Integer Getage () {return age;} public void Setage (Integer age) {this.age = age;} Public Integer GetId () {return ID;} public void SetId (Integer id) {this.id = id;}}

  

Studentcontroller.java

Package Springtest;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.modelattribute;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.servlet.ModelAndView; Import Org.springframework.validation.annotation.validated;import org.springframework.validation.bindingresult;@ Controllerpublic class Studentcontroller {@RequestMapping (value= "/student/index", method=requestmethod.get) public Modelandview student () {return new Modelandview ("Student_index2", "command", New Student ());} @ModelAttribute ("student") public student Createstudent () {return new student (); @RequestMapping (value= "/student/addstudent", method=requestmethod.post) public String addstudent (@ModelAttribute (" Student ") @Validated student Student,bindingresult result, model model) {if (Result.haserrors ()) {return" Student_index2 ";} Model.addattribute ("name", StUdent.getname ()); Model.addattribute ("Age", Student.getage ()); Model.addattribute ("id", Student.getid ()); return " Student_result2 ";}}

  

student_index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%><%@ taglib uri=" http://www.springframework.org/tags/form "prefix=" form " %><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

  

student_result.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" utf-8 "%><%@ page iselignored=" false "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

  

Spring mvc:hibernate Validator (field cannot be empty, in 1-150 itself)

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.