打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java – Spring MVC表单:options标签没有连接到我的对象Id?

我试图在列表框中显示我的命令对象集合字段.里面的集合是一个字段,id和名称.我想使用id作为html选项值,使用名称作为选项文本.见下面的代码;

<form:select id="customCollection" path="customCollection" size="10">    <form:options items="${command.customCollection}" itemValue="id" itemLabel="name"/></form:select>

名称打印正常,但值留空.这是输出HTML;

<option selected="selected" value="">name-value</option>

我最初的假设是我的数据不正确,但在我的页面中放入以下代码后;

<c:forEach items="${command.customCollection}" var="c">    ${c.id} : ${c.name} <br></c:forEach>

id和名称都正确打印出来.所以我的数据正确地传递给我的观点.这让我觉得我要么使用表单:选项不正确,要么在表单中遇到一些错误:选项.

有人可以帮我从这里出去吗?

编辑:
感谢BacMan和delfuego的帮助,我已经能够将这个问题缩小到我的活页夹.

以前我将元素中的值赋给行的名称,这是我的初始绑定器;

binder.registerCustomEditor(Collection.class, "customCollection",        new CustomCollectionEditor(Collection.class) {    @Override    protected Object convertElement(Object element) {        String name = null;        if (element instanceof String) {            name = (String) element;        }        return name != null ? dao.findCustomByName(name) : null;    }});

当我从initBinder方法中删除此代码时,行值正确地插入到表单中,但我需要一个customEditor将所述值转换为数据库对象.

所以这是我对活页夹的新尝试;

binder.registerCustomEditor(Collection.class, "customCollection",        new CustomCollectionEditor(Collection.class) {    @Override    protected Object convertElement(Object element) {        Integer id = null;        if (element instanceof Integer) {            id = (Integer) element;        }        return id != null ? dao.find(Custom.class, id) : null;    }});

但是,这会导致与上一个活页夹相同的行为并使值不显示.关于我在这里做错了什么的想法?

编辑2:
正如我上面提到的,如果我注释掉我的自定义绑定器,那么Custom对象会为表单的视图部分正确加载其id和名称,但是当我尝试保存它时,它永远不会绑定回父对象.所以我真的认为问题出在我的活页夹上.

我在我的convertElement方法中放置了调试语句.一切看起来都应该工作,dao正确地从数据库中提取对象.唯一令我怀疑的行为是每个Custom项都会调用convertElement方法两次.

解决方法:

这是我曾经理解出了什么问题的问题之一我不明白它是如何起作用的.

我完全以错误的方式使用CustomCollectionEditor.根据Marten Deinum在this thread,的帖子

As I stated in the other thread already the CustomCollectionEditor is to create Collections (List, Set, ?). So it will populate the desired collection with elements of the desired type.

However it is not intended to convert single elements into a value. It is designed to work on Collections, not on a single Role instance. You want 1 PropertyEditor to do 2 tasks for you.

因此,它为每个元素创建了一个唯一的集合,当它试图生成HTML时,最终在Spring代码中被淘汰了.

这就是我最终做的,

binder.registerCustomEditor(Custom.class,        new PropertyEditorSupport() {            @Override            public void setAsText(String text) {                Custom custom = dao.find(Custom.class,                        Integer.parseInt(text));                setValue(Custom);            }        });

我不知道为什么我以前的CustomCollectionEditor使用名称作为值.

来源:http://www.icode9.com/content-1-207301.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Cookie和Session专题
JSP与数据库的增删改查
节日不忘战备,以及那个实用的损管详图活页夹
jdbc的事务管理与ThreadLocale
活页夹
js表单处理中单选、多选、选择框值的获取及表单的序列化
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服