[英]Spring MVC + FreeMarker: How to render option tag?
Spring 3 MVC文檔聲明選項標簽可以像這樣呈現:
<tr>
<td>Country:</td>
<td>
<form:select path="country">
<form:options items="${countryList}" itemValue="code" itemLabel="name"/>
</form:select>
</td>
</tr>
我在Spring MVC中使用FreeMarker,所以我把它理解為:
<tr>
<td>Place:</td>
<td>
<@form.select path="place">
<@form.options items="${places}" itemValue="id" itemLabel="name"/>
</@form.select>
</td>
</tr>
當我點擊頁面時,我得到以下異常:
freemarker.core.NonStringException: Error on line 40, column 73 in event.ftl
Expecting a string, date or number here, Expression places is instead a freemarker.template.SimpleSequence
我應該在FreeMarker模板中使用什么代替$ {places},以便以上工作?
謝謝。
我一直在尋找完全相同的東西。 我不知道為什么Spring沒有包含在Spring的Freemarker宏庫中,但幸運的是它很容易實現:最佳實踐是啟動你自己的宏庫(例如mylib.ftl
)並將宏放在那里:
<#macro selectOptions path options key value>
<@spring.bind path/>
<select id="${spring.status.expression}" name="${spring.status.expression}">
<#list options as option>
<option value="${option[key]?html}"<@spring.checkSelected option[key]/>>${option[value]?html}</option>
</#list>
</select>
</#macro>
然后,您可以在Freemarker模板中使用新的宏,如下所示:
<#import "/mylib.ftl" as mylib />
...
<@mylib.selectOptions "country" countryList "code" "name" />
HTH
有點奇怪,但它的工作原理。
所以你會有
<@form.select path="place">
<@form.options items=places itemLabel="name"/>
</@form.select>
您可以嘗試以下(未經過個人測試)
<@form.select path="place">
<#list Request.places as place>
<@form.option value="${place}" label="name" />
</#list>
</@form.select>
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.