Jam*_*hon 6 spring hibernate jsp-tags spring-mvc
我正在尝试创建一个表单来编辑现有的数据库行.我正在使用Spring MVC表单标记将html自动绑定到表单后备对象.该行与另一个表有多对多的关系,我试图使用以下形式表示多个选择框:select tag;
<form:select path="rules">
<form:options items="${bundle.rules}" itemValue="name" itemLabel="name"/>
</form:select>
Run Code Online (Sandbox Code Playgroud)
我正在使用Hibernate进行持久化,因此该关系表示为Bundle pojo中的HashSet.
private Set<Rule> rules = new HashSet<Rule>(0);
Run Code Online (Sandbox Code Playgroud)
如果没有页面上的选择框,对象将正确更新到数据库,但是使用选择框,对象将不会更新到数据库,并且我在log4j日志中收到此错误,请注意此错误不会导致异常,它只在日志中可见;
DEBUG org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:256) - Data binding errors: 1
Run Code Online (Sandbox Code Playgroud)
无论我取消选择框内的项目,都会发生这种情况,整个表单拒绝正确提交.谁能帮我?
我知道如何将集合属性绑定到Spring MVC中的表单,这与此问题类似,遗憾的是,没有任何建议对我的问题有用.
问题在于提交表单.Spring无法绑定命令的对象,因此它不提交表单,而是将您重定向到formView.
成功执行绑定后,您将看到此消息:
No errors -> processing submit
Run Code Online (Sandbox Code Playgroud)
要解决您的问题,您需要在控制器中注册CustomCollectionEditor.(见此链接).它会是这样的:
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
{
binder.registerCustomEditor(Set.class, "rules", new CustomCollectionEditor(Set.class)
{
protected Object convertElement(Object element)
{
String name = "";
if (element instanceof String)
name = (String) element;
return name != null ? new Rule(name) : null;
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24964 次 |
| 最近记录: |