PDA

View Full Version : FormController question


radone
Jan 23rd, 2005, 08:12 AM
Can anyone help me? I am desperate about this problem :-(

I have 2 classes Town -*------1-> Country :

public class Town extends BaseObject implements Serializable {
protected String postalCode;
protected String name_i18n;
protected Country country;
// getters and setters
}


public class Country extends BaseObject implements Serializable {
protected String code;
protected String name_i18n;
// getters and setters
}

All DAO and Services works correctly, even whole Countr, even displaying Town in SpringMVC. Problem came when I try to save class Town - which compose class Country:

## Error message ##

Failed to convert property value of type [java.lang.String] to required
type [org.appfuse.model.Country] for property 'country'

## Error Logs ##

[appfuse] DEBUG [http-8080-Processor24]
TownFormController.formBackingObject(78) | entering 'formBackingObject'
method...
[appfuse] DEBUG [http-8080-Processor24]
SimpleFormController.processFormSubmission(218) | Data binding errors: 1

## TownFormController ##

public class TownFormController extends BaseFormController {
private TownManager townManager = null;

public void setTownManager(TownManager townManager) {
this.townManager = townManager;
}

public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'onSubmit' method...");
}
Town town = (Town) command;

Locale locale = request.getLocale();

if (request.getParameter("delete") != null) {
townManager.removeTown(town.getPostalCode().toStri ng());

saveMessage(request, getText("town.deleted", locale));
} else if (request.getParameter("update") != null) {

townManager.updateTown(town);

saveMessage(request, getText("country.updated", locale));

return new ModelAndView("redirect:editTown.html", "id", town
.getPostalCode());
} else {
try {
townManager.saveTown(town);
} catch (DataAccessException e) {
// error message
saveError(request, getText("town.exists", locale));
request.setAttribute("town", town);
return new ModelAndView(getFormView());
}
saveMessage(request, getText("country.added", locale));
}
return new ModelAndView(getSuccessView());
}

protected Object formBackingObject(HttpServletRequest request)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'formBackingObject' method...");
}
Country cc = new Country();
cc.setCode("CZ");

String id = request.getParameter("id");
Town town = null;

if (!StringUtils.isEmpty(id)) {
town = townManager.getTown(id);
return town;
} else {
town = new Town();
}
return town;
}
}


Any help will be very appreciated.

ojolly
Jan 23rd, 2005, 01:11 PM
The problem seems to come from the part which binds the request onto the command object.
See this log :

Failed to convert property value of type [java.lang.String] to required
type [org.appfuse.model.Country] for property 'country'

It means that a part of the request (of type String) should be converted to a Country (since you have a Town as command object with a setCountry) and that spring doesn't know how to do this. You should add a propertyEditor suitable to convert a String to a Country and register it in the initBinder method. That should do the trick.

radone
Jan 23rd, 2005, 02:39 PM
Thank you ojolly.

Does anyone know about some example or tutorial how to create propertyEditor?
I cant find anything usable.

erikw
Jan 23rd, 2005, 03:31 PM
Spring contains several PropertyEditors. For instance, CustomNumberEditor or CustomBooleanEditor, you could use those as an example.

Alef Arendsen
Jan 23rd, 2005, 05:16 PM
It's pretty simple: extend PropertyEditorSupport. There are two methods you should override: 'String getAsText()' and setAsText(String text). getAsText() can obtain the value it should transform to a string using 'Object getValue()' whereas setAsText(String text) should transform the text given to an Object and set it using setValue(Object). Something like this:


DatePropertyEditor extends PropertyEditorSupport {

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

void setAsText(String text) {
setValue(df.parse(text));
}

String getAsText() {
return df.format((Date)getValue());
}
}

irbouho
Jan 23rd, 2005, 10:37 PM
radone,
if country id is all you need in your controller, you can bind the form field to city.country.id binding of mapping it to city.country. That way, you do not need to create / use any specific PropertyEditorSupport. Spring does handle the binding to sub-objects properties out of the box.
HTH

radone
Jan 24th, 2005, 03:12 AM
Thanks

sherihan
Jan 25th, 2005, 03:20 AM
Hi radone,
If your problem was solved, would you plz send me a copy of your form controller and the form JSP because I'm facing the same problem.
Thanks in Advance.
Sherihan.