【发布时间】:2013-07-18 17:01:11
【问题描述】:
如何在 freemarker 模板中创建下拉字段?下拉选项和值将从地图传递。这是从 Controller 传递的地图:
Map<String, Map<String, String>> codeTable = new HashMap<String,Map<String, String>>();
Map<String, String> codeTableValues = new HashMap<String,String>();
codeTableValues.put("1", "US");
codeTableValues.put("2", "UK");
codeTableValues.put("3", "India");
codeTableValues.put("4", "Ireland");
codeTableValues.put("5", "Germany");
codeTable.put("country", codeTableValues);
对于 freemarker,我遇到了 spring.ftl 并尝试使用 formSingleSelect,但无法理解它。 FTL 代码:
<#elseif field.@type="select">
<@spring.bind "codeTable.country" />
<@spring.formSingleSelect "country", codeTable.country, "" />
</#if>
例外
FreeMarker template error: Method public org.springframework.web.servlet.support.BindStatus org.springframework.web.servlet.support.RequestContext.getBindStatus(java.lang.String) throws java.lang.IllegalStateException threw an exception when invoked on org.springframework.web.servlet.support.RequestContext object "org.springframework.web.servlet.support.RequestContext@1479ef9" with arguments of types [java.lang.String,]. See cause exception. The failing instruction (FTL stack trace): ---------- ==> #assign status = springMacroRequestCo... [in template "spring.ftl" in macro "bind" at line 74, column 17] #else [in template "spring.ftl" in macro "bind" at line 73, column 9] @spring.bind "codeTable.country" [in template "index.ftl" at line 31, column 33] #elseif field.@type = "select" [in template "index.ftl" at line 30, column 25]
【问题讨论】:
-
如果您想增加获得答案的机会,您应该复制粘贴该错误消息而不会丢失所有换行符。此外,失败的是
@spring.bind(所以也许你的问题可能更具体),但你没有包括原因异常。 -
感谢您的回复。我知道为什么
option会失败。但是我找不到任何使用 Map 创建下拉字段的示例。你能帮我完成那部分吗? -
加上我发现的唯一 spring.formSingleSelect 实现来自这个 SO question。stackoverflow.com/questions/11433510/…
-
好吧,我不是 Spring 用户,我只知道 FreeMarker。查看
spring.ftl,我发现第二个参数是Map。所以如果你传入的是Map,它应该可以工作。那么现在的错误信息是什么? -
周一会试用并通知您。
标签: spring spring-mvc freemarker