Pet*_*ete 5 java spring freemarker spring-mvc
这应该是非常基本的,但我在网上找不到任何关于它的东西,只是我看起来不能装在一起的点点滴滴......
我们正在使用带有freemarker的Spring MVC.现在我想在页面中添加一个表单,允许我从预定义列表中选择一个值(需要在后端访问数据库).
我的控制器:
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ModelAndView get(@PathVariable Integer id) {
// stuff..
ModelAndView mav = new ModelAndView();
mav.addObject("targetObject", new TargetObject());
mav.addObject("options", Arrays.asList("a", "b", "c"));
mav.setViewName("someview");
return mav;
}
Run Code Online (Sandbox Code Playgroud)
我发现freemarkers spring支持spring.ftl
,似乎我应该使用<@spring.formSingleSelect>
我尝试过这样的:
someView.ftl:
<#import "../spring.ftl" as spring />
<form action="somePath" method="POST">
<@spring.formSingleSelect "targetObject.type", "options", " " />
<input type="submit" value="submit"/>
</form>
Run Code Online (Sandbox Code Playgroud)
因此targetObject.type由宏看起来自动绑定.
但是如何让我的选项进入freemarker seequence以便宏可以创建选项?
现在我得到:
Expected collection or sequence. options evaluated instead to freemarker.template.SimpleScalar on line 227, column 20 in spring.ftl.
The problematic instruction:
----------
==> list options as value [on line 227, column 13 in spring.ftl]
in user-directive spring.formSingleSelect [on line 53, column 9 in productBase/show.ftl]
----------
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
<@spring.bind "${options}" />
Run Code Online (Sandbox Code Playgroud)
沿着这些方向的更多事情,但没有成功:
freemarker.core.NonStringException: Error on line 48, column 18 in someView.ftl
Expecting a string, date or number here, Expression options is instead a freemarker.template.SimpleSequence
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
经过多次改写和重新思考,我终于找到了解决方案:
第一
我需要有一个豆子明显地保持我的选择
第二
这些选项需要初始化为String列表,并由Spring MVC提供给页面:
public ModelAndView get() {
// ...
ModelAndView mav = new ModelAndView();
List<String> options = Arrays.asList(getOptionsFromDatabaseAndConvertToStringList());
mav.addObject("options",options );
mav.setViewName("someview");
return mav;
}
Run Code Online (Sandbox Code Playgroud)
第三
options
现在需要绑定在freemarker模板中,然后可以像任何其他freemarker变量一样访问(即无引号):
<@spring.bind "options" />
<form action="whatever" method="POST">
<@spring.formSingleSelect "targetBean.choice", options, " " />
<input type="submit" value="submit"/>
</form>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7250 次 |
最近记录: |