Yesterday we finished the data transmission, today we talk about the problem of data validation. Come, follow me to read: Count one oh shout, a press Yan.
650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0019.gif "alt=" J_0019.gif "/>
In Springmvc, the data is also very simple, spring3.0 has its own independent data validation Framework , while supporting the JSR303 Standard calibration framework.
Spring's DataBinder data binding, and the verification framework is also called to complete the data validation work.
The following steps are used:
1) Import the data check jar package
2) Add a checksum bean to the SPRINGMVC configuration file
3) Modify the entity class to add a checksum to the attribute
4) Modify yesterday's Login4 method, plus the relevant code of the checksum
5) Modify the JSP page, plus the error prompt
6) Enter address, test effect
Well, take a look at the detailed implementation, very simple drip! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0005.gif "alt=" J_0005.gif "/>
The first step : Copy the data validation jar package to the project's Lib directory (available for download in the attachment in this article).
Hibernate-validator-4.3.0.final.jar
Jboss-logging-3.1.0.cr2.jar
Validation-api-1.0.0.ga.jar
Step Two : Modify the SPRINGMVC configuration file and add the following code
<!--data Checksum-<bean id= "validator" class= " Org.springframework.validation.beanvalidation.LocalValidatorFactoryBean "></bean>
The third step : Modify the User entity class yesterday, plus the account and password test (the account can only be 8~20-bit letters, numbers or underscores, the password can be any character, the length must be in the 6~10 bit ).
@Pattern (regexp= "^\\w{8,20}$", message= "account can only be 8~20-bit letters, numbers or underscores") private String loginID; @Length (min=6, max=10, message= "Password length must be in 6~10 bit") Private String loginpwd;
Fourth Step : Modify the Login4 method, plus verify the relevant code.
@RequestMapping ("/login4") public modelandview login4 (@Valid @ModelAttribute ("user") User use, BindingResult bindingResult, HttpServletRequest request, Httpservletresponse response, httpsession session) { modelandview mav = new modelandview ("index.jsp"); //Judging Results if (Bindingresult.haserrors ()) { mav.setviewname ("login.jsp"); return mav; } &nbsP; if (Use.getloginid (). Equals ("admin") && use.getloginpwd (). Equals ("666")) { use.setnickname ("I am the Yellow River you are the Ocean"); //mav.addobject ("User", use); //adding data that needs to be passed to the next view session.setattribute ("User", use); } else{ mav.addobject ("msg", "pig brains, account numbers and passwords are all wrong!" "); mav.setviewname (" login.jsp "); //Modify View } return mav; }
Note: Precede the user object with @Valid annotation, and then add the bindingresult object after the object. The hasErrors method of the Bindingresult indicates whether the checksum is an error, or trueif there is an error.
Fifth Step : Modify the JSP page and use the SPRINGMVC label <form:errors> to display the error.
<!-- Add a reference to the form label at the top --><%@ taglib prefix= "form" uri= "http// Www.springframework.org/tags/form " %> <form:form modelattribute=" user " action= "Login4.form" method= "POST" > <!-- path: Indicates the inspection information (* Indicates all, if only want to display the validation information of a property, write the property name directly) CSSStyle: Represents the style of the area Element: Represents the container label used in this area --> <form:errors path= "*" cssstyle= "color:red;" element= "div" ></form:errors> account:<input Name= "loginID" type= "text" /><br /> Password: <input name= "loginpwd" type= "password" /><br /> <input type= "Submit" value= "Login" /> </form:form>
Sixth step : Visit the login.jsp page to test the effect of the error data.
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9E/84/wKioL1mSsvaiOsYdAAAsENFQqcQ996.png "title=" 11. PNG "alt=" Wkiol1mssvaiosydaaasenfqqcq996.png "/>
Here, the data check on the SPRINGMVC is OK.
Of course, the verification of the annotations is not only so, if you are interested, you can own Baidu.
What the!!!! You can't even Baidu!!!!!
Well, who said this is the introductory article, I help you Baidu bar ... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0016.gif "alt=" J_0016.gif "/>
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/9E/84/wKioL1mStOKDCtmFAABxg8Emhww000.png "style=" float : none; "title=" 12.png "alt=" Wkiol1mstokdctmfaabxg8emhww000.png "/>
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9E/96/wKiom1mStOPhgQoXAADD9OXbBDE412.png "style=" float : none; "title=" 13.png "alt=" Wkiom1mstophgqoxaadd9oxbbde412.png "/>
Enough meaning, also helped you become a picture, lest you save inconvenient ·, do not thank me, my surname is Ray, name proud days ... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0015.gif "alt=" J_0015.gif "/>
The most final friendship hint, the article has the data check to use the jar package , please download ha!
This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1956507
Java from getting started to giving up: SPRINGMVC data validation