<taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/spring.tld</taglib-location> </taglib><%@ include file="/WEB-INF/jsp/include.jsp" %><%@ taglib prefix="spring" uri="/spring" %> <html><head><title><fmt:message key="title"/></title></head><body><h1><fmt:message key="priceincrease.heading"/></h1><form method="post"> <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> <tr> <td alignment="right" width="20%">Increase (%):</td> <spring:bind path="priceIncrease.percentage"> <td width="20%"><input type="text" name="percentage" value="<c:out value="${status.value}"/>">
</td> <td width="60%"><font color="red"><c:out value="${status.errorMessage}"/></font>
</td> </spring:bind> </tr> </table> <br><spring:hasBindErrors name="priceIncrease">
<b>Please fix all errors!</b> </spring:hasBindErrors> <br><br> <input type="submit" alignment="center" value="Execute"></form><a href="<c:url value="hello.htm"/>">Home</a></body></html>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 percentage; public void setPercentage(int I) { percentage = I; logger.info(“Percentage set to “ + I); } public int getPercentage() { return percentage; } }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 PriceIncreaseValidator 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 too low.");
} } } public void setMinPercentage(int i) { minPercentage = i; } public int getMinPercentage() { return minPercentage; } public void setMaxPercentage(int i) { maxPercentage = i; } public int getMaxPercentage() { return maxPercentage; } }聯(lián)系客服