ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-05-27 01:51:01  阅读:337  来源: 互联网

标签:java spring-mvc spring forms jstl


我试图在列表框中显示我的命令对象集合字段.里面的集合是一个字段,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使用名称作为值.

标签:java,spring-mvc,spring,forms,jstl
来源: https://codeday.me/bug/20190527/1159812.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有