免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
java – Spring MVC表單:options標(biāo)簽沒有連接到我的對(duì)象Id?

我試圖在列表框中顯示我的命令對(duì)象集合字段.里面的集合是一個(gè)字段,id和名稱.我想使用id作為html選項(xiàng)值,使用名稱作為選項(xiàng)文本.見下面的代碼;

<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>

我最初的假設(shè)是我的數(shù)據(jù)不正確,但在我的頁面中放入以下代碼后;

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

id和名稱都正確打印出來.所以我的數(shù)據(jù)正確地傳遞給我的觀點(diǎn).這讓我覺得我要么使用表單:選項(xiàng)不正確,要么在表單中遇到一些錯(cuò)誤:選項(xiàng).

有人可以幫我從這里出去嗎?

編輯:
感謝BacMan和delfuego的幫助,我已經(jīng)能夠?qū)⑦@個(gè)問題縮小到我的活頁夾.

以前我將元素中的值賦給行的名稱,這是我的初始綁定器;

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

當(dāng)我從initBinder方法中刪除此代碼時(shí),行值正確地插入到表單中,但我需要一個(gè)customEditor將所述值轉(zhuǎn)換為數(shù)據(jù)庫對(duì)象.

所以這是我對(duì)活頁夾的新嘗試;

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

但是,這會(huì)導(dǎo)致與上一個(gè)活頁夾相同的行為并使值不顯示.關(guān)于我在這里做錯(cuò)了什么的想法?

編輯2:
正如我上面提到的,如果我注釋掉我的自定義綁定器,那么Custom對(duì)象會(huì)為表單的視圖部分正確加載其id和名稱,但是當(dāng)我嘗試保存它時(shí),它永遠(yuǎn)不會(huì)綁定回父對(duì)象.所以我真的認(rèn)為問題出在我的活頁夾上.

我在我的convertElement方法中放置了調(diào)試語句.一切看起來都應(yīng)該工作,dao正確地從數(shù)據(jù)庫中提取對(duì)象.唯一令我懷疑的行為是每個(gè)Custom項(xiàng)都會(huì)調(diào)用convertElement方法兩次.

解決方法:

這是我曾經(jīng)理解出了什么問題的問題之一我不明白它是如何起作用的.

我完全以錯(cuò)誤的方式使用CustomCollectionEditor.根據(jù)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.

因此,它為每個(gè)元素創(chuàng)建了一個(gè)唯一的集合,當(dāng)它試圖生成HTML時(shí),最終在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
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Cookie和Session專題
JSP與數(shù)據(jù)庫的增刪改查
節(jié)日不忘戰(zhàn)備,以及那個(gè)實(shí)用的損管詳圖活頁夾
jdbc的事務(wù)管理與ThreadLocale
活頁夾
js表單處理中單選、多選、選擇框值的獲取及表單的序列化
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服