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

  • Problems with Example application

    Hi. I´m trying to learn about springframework with the "step-by-step" application.

    I could finish the example without problems, and now, i want to have the form and the BD lis of products in the same jsp.

    For this page, i have a SimpleFormController, but when i implement the handleRequest method, i get the next exception.

    if i delete the Select form in the jsp, i do not have the excepcion.

    Code:
    org.apache.jasper.JasperException: Ha sucedido una excepción al procesar la página JSP /WEB-INF/jsp/hello.jsp en línea 20
    
    17:         <tr>
    18:           <td align="right" width="20%">Increase (%):</td>
    19:             <td width="20%">
    20:                 <form:select path="percentage">
    21:                     <form:option label="10" value="10" />
    22:                     <form:option label="20" value="20" />
    23:                     <form:option label="30" value="30" />
    
    
    Stacktrace:
        org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:413)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
        org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
        org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
        org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
        org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
        org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
        org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    causa raíz 
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'priceIncrease' available as request attribute
        org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:147)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:138)
        org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
        org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:409)
        org.springframework.web.servlet.tags.form.SelectTag.writeTagContent(SelectTag.java:198)
        org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
        org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
        org.apache.jsp.WEB_002dINF.jsp.hello_jsp._jspService(hello_jsp.java:139)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
        org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
        org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
        org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
        org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
        org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
        org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    this is my .jsp page:
    Code:
    <body>
        <h1><fmt:message key="heading"/></h1>
        <p><fmt:message key="greeting"/> <c:out value="${model.increase}"/></p>
        <h3>Products</h3>
        <c:forEach items="${model.products}" var="prod">
          <c:out value="${prod.description}"/> <i>$<c:out value="${prod.price}"/></i><br><br>
        </c:forEach>
        <br>
        <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:select path="percentage">
                        <form:option label="10" value="10" />
                        <form:option label="20" value="20" />
                        <form:option label="30" value="30" />
                        <form:option label="40" value="40" />
                        <form:option label="50" value="50" />
                    </form:select>
                </td>
                <td width="60%">
                  <form:errors path="percentage" cssClass="error"/>
                </td>
            </tr>
          </table>
          <br>
          <input type="submit" align="center" value="Execute">
        </form:form>
        <br>
      </body>
    here is my controller:

    Code:
    public class PriceIncreaseFormController extends SimpleFormController {
    
        private ProductManager productManager;
        
        @Override
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
    
            int increase= 0;
            if (request.getParameter("increase")!=null){
                increase= Integer.parseInt(request.getParameter("increase"));
            }
            Map<String, Object> myModel = new HashMap<String, Object>();
            myModel.put("increase", increase);
            myModel.put("products", this.productManager.getProducts());
    
            return new ModelAndView("hello", "model", myModel);
        }
    
        @Override
        public ModelAndView onSubmit(Object command)
                throws ServletException {
    
            int increase = ((PriceIncrease) command).getPercentage();
            productManager.increasePrice(increase);
    
            return new ModelAndView(new RedirectView(getSuccessView()), "increase", increase);
        }
    
        @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;
        }
    
    }
    and the .xml

    Code:
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="messages"/>
        </bean>
    
        <bean name="/hello.htm" class="springapp.web.PriceIncreaseFormController">
            <property name="sessionForm" value="true"/>
            <property name="commandName" value="priceIncrease"/>
            <property name="commandClass" value="springapp.service.PriceIncrease"/>
            <property name="formView" value="hello"/>
            <property name="successView" value="hello.htm"/>
            <property name="productManager" ref="productManager"/>
        </bean>
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>        
        </bean>
    Thanks for all.

  • #2
    Hi,

    when you bind an object using the form:form tag, this object must always be part of your model. When you hit the url, the controller's handleRequest method gets executed first; in this method, you override the Model object (in the return statement, you return a new model you create with the new command) and you don't provide a model object called "priceIncrease", which is expected by the form:form tag. Provide this object to the model in your handleRequest method and the error should go away.

    Comment


    • #3
      You shouldn't override the handleRequest method, that breaks the proper flow of the SimpleFormController. You should only implement the onSubmit, referenceData and formBackingObject methods. The handleRequest(Internal) should be left alone, else you break the flow.

      Comment


      • #4
        Thank you so much for your help

        You solve my problem.

        Comment

        Working...
        X