Цитата(afon @ 16.4.2010, 23:14 ) | | смахивает на то. что это н е реально |
Отвечу так профессионал и непрофессионал делает одинаковое количество ошибок. Только первый исправляет их быстро, второй не очень, а я не знаю где искать
Теперь скрины:
1. Ошибка : http://ipicture.ru/upload/100418/Pg3VRB4QFV.jpg 2. Проект (чтобы виден был priceincrease.jsp) http://ipicture.ru/upload/100419/3XEWLZ2dTp.png
3.Код взят отсюда: http://static.springsource.org/docs/Spring-MVC-step-by-step/part4.html
4. Код
PriceIncrease
| Код | package springapp.service;
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; }
}
|
PriceIncreaseValidator
| Код | package springapp.service;
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 PriceIncrease.class.equals(clazz); }
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; }
}
|
PriceIncreaseFormController
| Код | package springapp.web;
import org.springframework.web.servlet.mvc.SimpleFormController; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
import springapp.service.PriceIncrease; import springapp.service.ProductManager;
public class PriceIncreaseFormController extends SimpleFormController {
/** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass());
private ProductManager productManager;
@Override public ModelAndView onSubmit(Object command) throws ServletException {
int increase = ((PriceIncrease) command).getPercentage(); logger.info("Increasing prices by " + increase + "%.");
productManager.increasePrice(increase);
logger.info("returning from PriceIncreaseForm view to " + getSuccessView());
return new ModelAndView(new RedirectView(getSuccessView())); } @Override protected Object formBackingObject(HttpServletRequest request) throws ServletException { PriceIncrease priceIncrease = new PriceIncrease(); priceIncrease.setPercentage(20); return priceIncrease; }
public void setProductManager(ProductManager productManager) { this.productManager = productManager; }
public ProductManager getProductManager() { return productManager; }
}
|
priceincrease.jsp
| Код | <%@ include file="/WEB-INF/jsp/include.jsp" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html> <head> <title><fmt:message key="title"/></title> <style> .error { color: red; } </style> </head> <body> <h1><fmt:message key="priceincrease.heading"/></h1> <form:form method="post" commandName="priceIncrease"> <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> <tr> <td align="right" width="20%">Increase (%):</td> <td width="20%"> <form:input path="percentage"/> </td> <td width="60%"> <form:errors path="percentage" cssClass="error"/> </td> </tr> </table> <br> <input type="submit" align="center" value="Execute"> </form:form> <a href="<c:url value="hello.htm"/>">Home</a> </body> </html>
|
include.jsp
| Код | <%@ page session="false"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
web.xml
| Код | <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>SpringMVConTomcat</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVConTomcat</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> <jsp-config> <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/tld/spring-form.tld</taglib-location> </taglib> </jsp-config> </web-app>
|
|