2011-06-22 50 views
0

很簡單的問題。如果我有一個字符串列表,我在通過Springs窗體下拉列表中顯示:options標籤,我如何將title屬性的值設置爲字符串值?春天窗體:選項標題從字符串

<form:options items="${listOfString}" title=" ?? "/> 

另外我會做一個forEach,但它可以做一個窗體:選項標記?

謝謝!

+0

,我猜這不能做

您可以找到窗體後面的代碼? :) – Hoof

+0

你是說你想讓標題成爲列表中所有字符串的連接嗎?或者是其他東西? – atrain

回答

0

你只是省略了「標題」屬性:

<form:options items="${listOfString}"/> 
+0

嗯..你其實是指'標題'的財產或選項標籤? – Dzmitry

0

我假設你的意思是還有的itemLabelitemValue PARAMS和你也想有一個itemTitle PARAM所以你可以指定包含字符串的對象上的字段名稱將變爲title =「」屬性。

所以關於這個問題:https://jira.springsource.org/browse/SPR-7648

如果是這樣的話,我發現我不得不推出自己的解決方案。這裏的.tag文件,我寫這樣做:

<%@ tag language="java" pageEncoding="ISO-8859-1"%> 
<%@ attribute name="items" type="java.util.Collection" required="true" %> 
<%@ attribute name="itemLabel" type="java.lang.String" required="true" %> 
<%@ attribute name="itemValue" type="java.lang.String" required="true" %> 
<%@ attribute name="itemTitle" type="java.lang.String" required="true" %> 
<%@ attribute name="selectedValue" type="java.lang.String" required="true" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<c:forEach var="entry" items="${items}"> 
    <c:set var="selectedAttrString" value="${entry[itemValue] == selectedValue ? 'selected=\"selected\"' : ''}" /> 
    <option value="${entry[itemValue]}" label="${entry[itemLabel]}" title="${entry[itemTitle]}" ${selectedAttrString} /> 
</c:forEach> 

我還包含能夠設置選定的項目。我省略了htmlEscape和css相關的參數,因爲我不需要它們,但是如果需要可以輕鬆地添加它們。

注意:很酷的部分是,SPeL允許您使用字符串(很像Javascript)來處理字段名稱,所以如果我們假設itemValue = "id",那麼entry[itemValue]的計算結果爲entry.id。整潔的嘿?這裏順便說一句選項標籤:所以​​