java - Spring MVC 窗体 :options tag not wiring into my objects Id?

标签 java spring forms spring-mvc 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 和 name 都被正确打印出来了。所以我的数据正确地传送到我的 View 。这让我假设我要么错误地使用了 form:options,要么遇到了 form:options 中的一些错误。

有人能帮我吗?

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

之前我将元素中的值分配给行的名称,这是我的初始 Binder ;

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 的新尝试;

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;
    }
});

然而,这会导致与以前的 Binder 相同的行为,并使值不显示。关于我在这里做错了什么有什么想法吗?

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

我已将调试语句放入我的 convertElement 方法中。一切看起来都应该正常工作,dao 正确地从数据库中提取对象。 唯一让我怀疑的行为是每个自定义项的 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 窗体 :options tag not wiring into my objects Id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1928929/

相关文章:

java - Spring mvc加载资源的问题

java - 如何在简单的 Spring Boot 应用程序中 Autowiring 扩展 CrudRepository 的存储库?

javascript - 如何打开一个弹出文本框要求用户提交评论?

javascript - 使用表单中的提交链接发送值

Java : divide the screen

java - 当尝试使用 eclipse juno 服务器运行 struts2 时,出现 HTTP Status 404 错误

java - Spring Boot 可选的多部分 POST 请求

jquery - jqgrid编辑表单默认自动完成="off",如何更改为自动完成="on"

java - 在 JAVA 中使用 XSLT 将 XML 元素从小写转换为大写

java - 如何从另一个类获取字符串? - 安卓