Java 表单和属性编辑器中的预选选项
我在一个web应用程序中使用Spring,使用表单,现在它工作得很好,但我想改变一件事,那就是当我遇到错误时。 我有一个小表单,有两个文本字段和一个选择选项字段,正是这个选择字段导致了错误。选择选项从数据库中填充 我的代码: edit.jsp 当我编辑一个已经存在的帖子时,我希望在表单中预选家长,而这是我无法完成的事情 有人能帮我吗? 非常感谢您抽出时间,我希望我能有所收获。 多谢各位 *编辑*** 我现在将编辑器改为格式化程序,但运气不好:Java 表单和属性编辑器中的预选选项,java,spring-mvc,spring-mvc-initbinders,Java,Spring Mvc,Spring Mvc Initbinders,我在一个web应用程序中使用Spring,使用表单,现在它工作得很好,但我想改变一件事,那就是当我遇到错误时。 我有一个小表单,有两个文本字段和一个选择选项字段,正是这个选择字段导致了错误。选择选项从数据库中填充 我的代码: edit.jsp 当我编辑一个已经存在的帖子时,我希望在表单中预选家长,而这是我无法完成的事情 有人能帮我吗? 非常感谢您抽出时间,我希望我能有所收获。 多谢各位 *编辑*** 我现在将编辑器改为格式化程序,但运气不好: @Component public class Ca
@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;
}
}
我做错了什么?!?
(我想稍后在此添加一个验证器,这就是从编辑器更改为格式化程序的原因)
@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));
}
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);
}
}
}
@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;
}
}
Formatter Print with ID=1
Formatter Parse with ID=0
Formatter Print with ID=1
Formatter Print with ID=8