Results 1 to 6 of 6

Thread: simpleformcontroller problem

  1. #1

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Your code looks ok, also your jsp doesn't do anything with the model, so I don't really see your issue here.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default binding error

    Can you show the error?

  4. #4

    Default

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

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6

    Default

    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;
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •