Announcement Announcement Module
Collapse
No announcement yet.
simpleformcontroller problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • simpleformcontroller problem

    In my app I'm using a simpleformcontroller. The problem is that when I set the formView-tag in the dispatcher-servlet I'm not able to add the model-object in the jsp-file. Can anyone explain how to do this?

    I tried to let the referenceData method return a populated model-object, but it doesn't work.

    I also tried to overwrite the showForm method, but then I always got a binding error. Can anyone help?

    Relevant code in Dispatcher-servlet
    Code:
    <bean id="RegHotelFormController" class="web.controllers.RegHotelFormController">
        <property name="sessionForm" value="true"/>
        <property name="commandName" value="hotel"/>
        <property name="commandClass" value="db.Hotel"/>
        <property name="formView" value="user/regHotelTest"/>
      </bean>
    Relevant code in RegHotelFormController
    Code:
    RegHotelFormController extends SimpleFormController {
      
      @Override
      protected Object formBackingObject(HttpServletRequest request) throws ServletException {
         
        Hotel h = new Hotel();
        long l = 4;
        h.setHotelID(l);
        h.setHotelType("Hotel");
        return h;
      }
    
      @Override
      protected Map referenceData(HttpServletRequest request, Object command, Errors errors) {
       
        Map model = new HashMap();
        model = getRightframe().addComponents(model);
    
        return model;
      }
    }

    Relevant code in regHotelTest.jsp

    Code:
    <form:form method="post"  commandName="hotel">
               <table>
                  <tr>
                    <td><label >Hoteltype:</label></td>
                    <td><form:input path="hotelType"/></td>
                  </tr>    
                </table>            
              </form:form>

  • #2
    Your code looks ok, also your jsp doesn't do anything with the model, so I don't really see your issue here.

    Comment


    • #3
      binding error

      Can you show the error?

      Comment


      • #4
        The binding is ok. The problem is that I can't use the objects I put into the model-object...

        Comment


        • #5
          The problem is that I can't use the objects I put into the model-object...
          In the code displayed you no where reference any object in your model. Als without seeing HOW your model is being build and what is being put in it is hard to say why is doesn't work. So post the relevant jsp and model filling code. That way we can see if there is some mismatch between your code and the jsp.

          Comment


          • #6
            I fixed it... The problem was that I referenced the model-object in the jsp with: model.hotel.hotelID, while I should reference it hotel.hotelID.
            Thanks for your help.

            Relevant code in regHotelTest.jsp
            Code:
            <h5><c:out value="${model.hotel.hotelID}"/></h5>
            <form:form method="post" commandName="hotel">
            <table>
              <tr>
                <td><label >Hoteltype:</label></td>
                <td><form:input path="hotelType"/></td>
              </tr>
            </table>
            </form:form>
            Relevant code in RegHotelFormController
            Code:
            @Override
              protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response,
                org.springframework.validation.BindException errors, Map controlModel) throws Exception {
            
                Hotel h = new Hotel();
                long l = 6;
                h.setHotelID(l);
                h.setHotelType("Hotel");
            
                ModelAndView mav = new ModelAndView(getFormView());
                Map model = mav.getModel();
                model.put("hotel", h);
            
                model = getRightframe().addComponents(model);
               
                return mav;
              }
            Hotel.java

            public class Hotel{

            private Long hotelID;
            private String hotelType;

            public Long getHotelID() {
            return hotelID;
            }

            public void setHotelID(Long hotelID) {
            this.hotelID = hotelID;
            }

            public String getHotelType() {
            return hotelType;
            }

            public void setHotelType(String hotelType) {
            this.hotelType = hotelType;
            }
            }

            Comment

            Working...
            X