I'm working with Spring in a web application, using forms and right now it's working fine, but i'd like to change one thing and that's when I get an error. I have a small form with two text fields and one select option field and it's this select field that causes the errors. The select option is populated from the database.
My code: edit.jsp
<tr>
<th><label for="parent"></label></th>
<td><sf:select path="parent">
<sf:option value="0" label="Parent" />
<sf:options items="${parentsList}" itemLabel="name" itemValue="ID" />
</sf:select></td>
</tr>
controller.java
@RequestMapping(value = "/edit", method = RequestMethod.GET, params = {"id"})
public String edit(Model model, @RequestParam("id") int id) {
try {
if (logger.isInfoEnabled()) {
logger.info("Edit category with id {}", id);
}
model.addAttribute("heading", "Edit Category");
model.addAttribute("parentsList", listOfParents());
model.addAttribute(categoryService.getCategory(id));
if (logger.isInfoEnabled()) {
logger.info("Finished");
}
} catch (DataAccessException ex) {
logger.error(ex.getMessage(), ex);
}
return "category/edit";
}
private List<Category> listOfParents() {
List<Category> listOfParents = new ArrayList<Category>();
try {
listOfParents.addAll(categoryService.listCategoryParents());
} catch (DataAccessException ex) {
logger.error(ex.getMessage(), ex);
}
return listOfParents;
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Category.class, "parent", new CategoryEditor(CategoryService));
}
My editor.java
public class CategoryEditor extends PropertyEditorSupport {
private CategoryService categoryService;
public CategoryEditor(CategoryService categoryService) {
this.categoryService = categoryService;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text.equals("0")) {
this.setValue(null);
} else {
category c = categoryService.getCategory(Integer.parseInt(text));
this.setValue(sc);
}
}
}
When I edit an already existing post I would like to have the parent preselected in the form and this is something I just can't get to work.
Is there anyone who can help me? Thank you so much for your time and I hope I make any sence. Thank you.
*EDIT*** I have now changed my editor to a formatter instead with no luck:
@Component
public class CategoryFormatter implements Formatter<Category> {
@Override
public String print(Category parent, Locale locale) {
System.out.println("Formatter Print with ID="+parent.getID());
return Integer.toString(parent.getID());
}
@Override
public Category parse(String id, Locale locale) throws ParseException {
Category parent = new Category();
parent.setID(Integer.parseInt(id));
System.out.println("Formatter Parse with ID="+parent.getID());
return parent;
}
}
But I still doesn't get the existing value in my drop down when I edit the object. My print outs on edit object prints:
Formatter Print with ID=1
Formatter Parse with ID=0
Formatter Print with ID=1
Formatter Print with ID=8
What am I doing wrong?!? (I would like to add a validator to this later on that's the reason for changing from editor to formatter)