PDA

View Full Version : simpleformcontroller problem


vator
Jun 8th, 2008, 08:35 AM
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
<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
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
<form:form method="post" commandName="hotel">
<table>
<tr>
<td><label >Hoteltype:</label></td>
<td><form:input path="hotelType"/></td>
</tr>
</table>
</form:form>

Marten Deinum
Jun 9th, 2008, 03:10 AM
Your code looks ok, also your jsp doesn't do anything with the model, so I don't really see your issue here.

praedos
Jun 9th, 2008, 03:36 AM
Can you show the error?

vator
Jun 9th, 2008, 04:18 AM
The binding is ok. The problem is that I can't use the objects I put into the model-object...

Marten Deinum
Jun 9th, 2008, 04:43 AM
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.

vator
Jun 9th, 2008, 04:59 AM
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

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