Announcement Announcement Module
Collapse
No announcement yet.
Problems with spring:bind Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with spring:bind

    I'm pretty new to Spring programming, and I've tried to make my way through the Thomas Risberg's "Developing a Spring Framework MVC application step-by-step".

    When I'm running the app, the errormessages won't show up, and I can't figure out what's wrong...

    It won't show my success.jsp file. The only thing that happens is that it shows the file LoginTest.jsp without any errormessages.
    There are no errormessages in the log file either.

    Below is some of my code:

    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" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>



    LoginTest.jsp

    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>

    <%@ include file="/WEB-INF/lib/include.jsp" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head><title><fmt:message key="title"/></title></head>
    <body>
    <h1><fmt:message key="priceincrease.heading"/></h1>
    <form method="get">
    <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>


    Dispatcher-servlet

    <bean id="priceIncreaseValidator" class="bus.PriceIncreaseValidator"/>
    <bean id="priceIncreaseForm" class="web.controllers.SpringTestController">
    <property name="sessionForm"><value>true</value></property>
    <property name="commandName"><value>priceIncrease</value></property>
    <property name="commandClass"><value>bus.PriceIncrease</value></property>
    <property name="validator"><ref bean="priceIncreaseValidator"/></property>
    <property name="formView"><value>loginTest</value></property>
    <property name="successView"><value>success</value></property>
    <property name="productManager">
    <ref bean="prodMan"/>
    </property>
    </bean>


    <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/priceincrease.htm">priceIncreaseForm</prop>
    </props>
    </property>
    </bean>


    <bean id="prodMan" class="bus.ProductManager">
    <property name="products">
    <list>
    <ref bean="product1"/>
    <ref bean="product2"/>
    <ref bean="product3"/>
    </list>
    </property>
    </bean>


    SpringTestController

    public class SpringTestController extends SimpleFormController {

    /** Logger for this class and subclasses */
    protected final Log loggerObj = LogFactory.getLog(getClass());

    private ProductManager prodMan;

    @Override
    public ModelAndView onSubmit(Object command)
    throws ServletException {

    int increase = ((PriceIncrease) command).getPercentage();
    loggerObj.info("Increasing prices by " + 6 + "%.");

    prodMan.increasePrice(increase);

    String now = (new java.util.Date()).toString();
    loggerObj.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);

    loggerObj.info("Er i formbacking");


    return priceIncrease;
    }
    }

  • #2
    When posting code, please use the [ code][/code ] tags, that way your code remains more readable.

    Your jsp is wrong. You don't use the ${status.expression} for binding, so nothing is going to bind. I also suggest that instead of the spring:bind tags you use the form tags. They make your life easier.

    Code:
    <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>
    Code:
    <spring:bind path="priceIncrease.percentage">
      <td width="20%">
        <input type="text" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">
      </td>
      <td width="60%">
        <font color="red"><c:out value="${status.errorMessage}"/></font>
      </td>
    </spring:bind>
    Next to that there are some issues with the tutorial you mentioned, it was made for Spring 1.2 at the meantime we are already at version 2.5. There is a newer/revised version on the Spring Framework website.

    Comment


    • #3
      Thanks mdeinum!!

      Solved the problem!!

      Comment

      Working...
      X