繁体   English   中英

Spring MVC + FreeMarker:如何呈现选项标签?

[英]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

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

  • 删除$ {“places”}并将其作为地方使用
  • 删除itemid,让spring为你处理它

所以你会有

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM