font>
td>
spring: bind>
TR>
table>
please fix all errors! b>
Spring: HasbinderRors>
form>
> Home
body>
html>
l Binding text fields to the properties of the Command object (later introduction) (specified by the Path property), status.value variable indicates the value of the binding property, the status.errorMessage variable indicates the error message returned when the error is verified.
l Binding error to the specified object (specified by the Name property)
l Command object is a simple JavaBean, passed to the validator, if verified, then pass to the controller
Package bus;
Import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfactory;
Public class priceincrease {
/ ** Logger for this class and subclasses * /
Protected final log logger = logfactory.getlog (getclass ());
Private int initage;
Public void setpercentage (int i) {
Percentage = i;
Logger.info ("Percentage Set To" i);
}
Public int getpercentage () {
Return percentage;
}
}
l When the user submits the form, the data of the form is set to the Command object, and the verifier is called for data validity verification.
Package bus;
Import org.springframework.validation.validator;
Import org.springframework.validation.errors;
Import org.apache.commons.logging.log;
Import org.apache.commons.logging.logfactory;
Public Class PriceSevalValidator Implements Validator {
PRIVATE INT Default_min_Percentage = 0;
PRIVATE INT Default_max_percentage = 50;
Private Int minpercentage = default_min_percentage;
Private int maxpercentage = default_max_percentage;
/ ** Logger for this class and subclasses * /
Protected final log logger = logfactory.getlog (getclass ());
Public Boolean Supports (Class Clazz) {
Return Clazz.equals (PriceIncrease.class);
}
Public void validate (Object obj, errors errors) {PriceIncRease Pi = (PriceIncrease) OBJ;
IF (pi == null) {
Errors.rejectValue ("Percentage", "Error.Not-Specified", NULL, "Value Required.");
}
Else {
Logger.info ("Validating with" PI ":" Pi.GetPercentage ());
IF (Pi.GetPercentage ()> maxpercentage) {
Errors.rejectValue ("Percentage", "Error.Too-high",
New Object [] {new integer (maxpercentage)}, "Value Too High.");
}
IF (Pi.GetPercentage () <= minpercentage) {
Errors.rejectValue ("Percentage", "Error.Too-Low",
New Object [] {new integer (minpercentage)}, "value to low.");
}
}
}
Public void setminpercentage (INT i) {
MinPercentage = i;
}
Public int getminpercenetage () {
Return minpersCentage;
}
Public void setMaxpercentage (int i) {
Maxpercentage = i;
}
Public int getMaxPercentage () {
Return Maxpercentage;
}
}
l The verifier implements the Validator interface, which has two methods:
Ø Supports (): Decide if the object specified can verify
Ø Validate (): Implement the validation object, provide two parameters, one is the verification object, one is an error message we build the error message related to the domain property; error message can be obtained using the Status.ErrorMessage variable introduced earlier
l Errors object's rejectValue () method is used to pop up the specified error description of the verification object specified domain properties, which contains four parameters: property name, message key value (defined in the property file), message parameters (providing dynamic messages) and Default message
转载请注明原文地址:https://www.9cbs.com/read-126659.html