Hi folks,
I'm having an issue with PropertyEditorSupport. I've got 2 classes Photo and Category. Then I have a form, you can fill in name and select category from the selectbox.
The jsp part of the form :
I have registered my CustomEditor :PHP Code:<spring:bind path="photo.category">
<label for="<c:out value="${status.expression}"/>"><fmt:message key="photoform.category.name"/></label>
<select name="<c:out value="${status.expression}"/>">
<c:forEach var="category" items="${categories}"> value="<c:out value="${category.id}"/>"><c:out value="${category.name} ${status.value}"/></option>
</c:forEach>
</select>
</spring:bind>
The property editor:PHP Code:protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
binder.registerCustomEditor(Category.class, new CategoryEditor(categoryDao));
}
During the form setup - in formBackingObject I can see the call of getAsText, but when I submit the form setAsText is not called. It also genereates an exception:PHP Code:public class CategoryEditor extends PropertyEditorSupport{
protected final Log logger = LogFactory.getLog(getClass());
private CategoryDao categoryDao;
public CategoryEditor(CategoryDao categoryDao) {
this.categoryDao = categoryDao;
}
public String getAsText() {
Category cat = (Category) getValue();
logger.info("Converting to int byId: " +cat.getId());
return Integer.toString(cat.getId());
}
public void setAsText(String[] text) {
logger.info("in setAsText" + text.toString());
Category cat;
cat=categoryDao.getCategoryById(Integer.valueOf(text[0]));
logger.info("Setting category "+text +" name: " +cat.getName());
setValue(cat.getId());
}
}
Field error in object 'photo' on field 'category': rejected value [[Ljava.lang.String;@1fdde41]; codes [typeMismatch.photo.category,typeMismatch.category, typeMismatch.org.power_soft.photobook.bus.Category ,typeMismatch]; arguments [org.springframework.context.support.DefaultMessage SourceResolvable: codes [photo.category,category]; arguments []; default message [category]]; default message [Failed to convert property value of type [java.lang.String[]] to required type [photobook.bus.Category] for property 'category'; nested exception is java.lang.IllegalArgumentException: 58,72]
58 is the default value, 72 is the new one. I can see that String array is returned instead of string, I reckon that is the reason why it doesn't call setAsText. Unfortunately I have no clue how to get around this
any ideas why it returns the array and/or how to deal with it?
thanks Vitek


Reply With Quote