SPRINGMVC form Annotations Simple validation

Source: Internet
Author: User


SPRINGMVC form label <%@ taglib prefix= "form" uri= "Http://www.springframework.org/tags/form"%>

On the page
<form:input path= "AccountName"/>
<form:errors path= "AccountName" cssstyle= "color:red"/>

How does the data passed by the page bind to the project entity?
1, adding commandname= "user" to the form label in the JSP means that you want to bind the contents of this tag to
You can also use modelattribute= "student" to label the converted object in JSP

2. Identify the entities that need to be bound
Account name
@NotEmpty (message= "Accountnamenotempty") tips to be displayed on the page
Private String AccountName;

Password
@NotEmpty (message= "Passwordnotempty")
private String password;

3, in the controller
@RequestMapping (value = "/show", method = Requestmethod.post)
Public String Show (@Valid @ModelAttribute ("user") user user, Errors validerrors, Modelmap modelmap) {

System.out.println ("AccountName" + user.getaccountname ());
System.out.println (Validerrors.haserrors ());

Validation of validation data
if (Validerrors.haserrors ()) {
return "Login";
}

@Valid @ModelAttribute ("user") user user, Errors validerrors,
@Valid for entities that require validation
@ModelAttribute ("user") explicitly specifies which entity to validate, followed by the entity
Errors Validerrors, immediately behind @Valid


5, also need to be in the controller

Bind Login
@ModelAttribute ("User")
Public User GetUser () {
return new User ();
}
The method that is @ModelAttribute annotated is executed before each method of the controller is executed, so use it sparingly for the use of a controller to map multiple URLs
Represents injecting an entity class

SPRINGMVC form Annotations Simple validation

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.