The application will present simple user registration form to the user. Form will have the following fields:
1. User Name
2. Age
3. Password
The validator framework will validate the user input. If there is any validation error application will display the appropriate message on the form.
To use the validation framework in the application "hibernate-validator-4.0.2.GA.jar" and " validation-api-1.0.0.GA.jar" must be added to the project libraries.
Application uses following jar files:
Step 1:
Create index.jsp under WebContent . The code index.jsp as :
|
In the above page we are creating a new link to access the Validation Form example from the index page of the application.
Step 2:
Now we will create a model class " ValidationForm.java" under src folder . The code of "ValidationForm.java" is:
package net.roseindia.form;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
public class ValidationForm {
@NotEmpty
@Size(min = 1, max = 20)
private String userName;
@NotNull
@NumberFormat(style = Style.NUMBER)
@Min(1)
@Max(110)
private Integer age;
@NotEmpty(message = "Password must not be blank.")
@Size(min = 1, max = 10, message = "Password must between 1 to 10 Characters.")
private String password;
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
In the above class we have added the proper annotation for validating the form values. Here is the list of annotation used for validation:
@NotEmpty
@Size(min = 1, max = 20)
@NotNull
@NumberFormat(style = Style.NUMBER)
@Min(1)
@Max(110)
@NotEmpty(message = "Password must not be blank.")
@Size(min = 1, max = 10, message = "Password must between 1 to 10 Characters.")
Step 3 :
Now create a folder views under WebContent/WEB-INF . Again cerate "validationform.jsp" under WebContent/WEB-INF/views as :
|
In this above jsp file <form:errors path="..." /> tag is used to display the validation error messages.
Step 4:
Now create "validationsuccess.jsp" file under WebContent/WEB-INF/views . The code of "validationsuccess.jsp" is :
|
Step 5:
Now create Contoller class "ValidationController.java" under src folder. Controller class use for RequestMapping and process the user request. The code of "ValidationController.java" as:
package net.roseindia.controllers;
import net.roseindia.form.ValidationForm;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/validationform.html")
public class ValidationController {
// Display the form on the get request
@RequestMapping(method = RequestMethod.GET)
public String showValidatinForm(Map model) {
ValidationForm validationForm = new ValidationForm();
model.put("validationForm", validationForm);
return "validationform";
}
// Process the form.
@RequestMapping(method = RequestMethod.POST)
public String processValidatinForm(@Valid ValidationForm validationForm,
BindingResult result, Map model) {
if (result.hasErrors()) {
return "validationform";
}
// Add the saved validationForm to the model
model.put("validationForm", validationForm);
return "validationsuccess";
}
}
The @RequestMapping annotation is used for request uri mapping.
Step 6:
Now modify web.xml as:
|
Step 7:
Again create "dispatcher-servlet.xml" under WebContent/WEB-INF . The "dispatcher-servlet.xml" code as :
|
Where <mvc:annotation-driven> is used for annotation driven controllers and validation. While message Resource configuration is done in "dispatcher-servlet.xml" using following entry:
|
Again create customization Message file "messages.properties" under WebContent/WEB-INF. The modify "messages.properties" as
NotEmpty.validationForm.userName=User Name must not be blank. Size.validationForm.userName=User Name must between 1 to 20 characters. |
Step 8:
Now run Application display output as :
if click Validation Form hyperlink then open validationform as :
Step 9:
Now Enter userName, Age and Password if find any error display as:
Again display error-
If validation success then display validationsucess page as :
标签:validationForm,under,Step,mvc,效验,import,spring3,validation,public From: https://blog.51cto.com/u_16087105/6254545