2010-07-10 22 views
1

弹簧3 MVC文档指出选项标签,可以呈现:Spring MVC + FreeMarker:如何渲染选项标签?

<tr> 
     <td>Country:</td> 
     <td> 
      <form:select path="country"> 
       <form:options items="${countryList}" itemValue="code" itemLabel="name"/> 
      </form:select> 
     </td> 
</tr> 

我使用的FreeMarker与Spring MVC,所以我将其解释为:

<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模板,这样上面的作品是什么?

谢谢。

回答

1

你可以尝试以下的(没有亲自测试)

<@form.select path="place"> 
    <#list Request.places as place> 
     <@form.option value="${place}" label="name" /> 
    </#list> 
</@form.select> 

希望这有助于!

2

我一直在寻找完全一样的东西。我不知道为什么这不包含在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

1

有点怪异,但它的工作原理。

  • 删除$ { “地方”},并用它作为straigh地方
  • 删除的itemid,让春天为您处理

所以你必须

<@form.select path="place"> 
    <@form.options items=places itemLabel="name"/> 

</@form.select>