下拉框 - 从Spring MVC模型/上下文到使用freemarker形成 [英] Dropdown box - from Spring MVC model / context to form using freemarker
问题描述
这应该是非常基本的,但我在网上找不到任何关于它的东西,只是我看起来不能装在一起的点点滴滴..
<我们正在使用带有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);
返回mav;
}
我发现freemarkers spring支持 spring.ftl
似乎我应该使用< @ spring.formSingleSelect>
我试过这样:
someView.ftl:
< #import../spring.ftl 作为春天/>
< form action =somePathmethod =POST>
< @ spring.formSingleSelecttargetObject.type,options,/>
< input type =submitvalue =submit/>
< / form>
因此targetObject.type由宏看起来自动绑定。
但是如何让我的选项进入freemarker seequence以便宏可以创建选项?
现在我得到:
预期的收集或序列。在spring.ftl的第227行第20行,将选项评估为freemarker.template.SimpleScalar。
有问题的说明:
----------
==>将选项列为值[在第227行,spring.ftl第13列]用户指令spring.formSingleSelect中的
[在第53行,productBase / show.ftl第9列]
----- -----
我也试过:
< @ spring.bind$ {options}/>
以及更多内容但没有成功:
freemarker.core.NonStringException:someView.ftl
第48行第18行出错在这里期待字符串,日期或数字,表达式选项是freemarker .template.SimpleSequence
感谢您的帮助!
经过多次改写和重新思考后,我终于找到了解决方案:
第一次
我需要明确保留我的选择
第二
这些选项需要初始化为String列表,并由Spring MVC提供给页面:
public ModelAndView get(){
// ... ...
ModelAndView mav = new ModelAndView();
List< String> options = Arrays.asList(getOptionsFromDatabaseAndConvertToStringList());
mav.addObject(options,options);
mav.setViewName(someview);
返回mav;
}
第三
选项
现在需要绑定在freemarker模板中,然后可以像任何其他freemarker变量一样进行访问(即否引号):
< @ spring.bindoptions/>
< form action =whatevermethod =POST>
< @ spring.formSingleSelecttargetBean.choice,options,/>
< input type =submitvalue =submit/>
< / form>
This should be very basic but I can't find anything about it in the web, just bits and pieces that I don't seem able to fit together..
We're using Spring MVC with freemarker. Now I want to add a form to my page that allows me to select a value from a predefined list (requires database access in the backend).
My controller:
@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;
}
I found freemarkers spring support spring.ftl
and it seems I should use <@spring.formSingleSelect>
which I've tried like this:
someView.ftl:
<#import "../spring.ftl" as spring />
<form action="somePath" method="POST">
<@spring.formSingleSelect "targetObject.type", "options", " " />
<input type="submit" value="submit"/>
</form>
So targetObject.type is bound automatically by the macro it seems.
But how do I get my options into a freemarker seequence so that the macro can create the options?
Right now I get:
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]
----------
I also tried:
<@spring.bind "${options}" />
and more things along those lines but with no success:
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
Thank's for any help!
After much rephrasing and rethinking I finally discovered the solution:
First
I need to have a bean hold my choice obviously
Second
The options need to be initialized as a String list and provided by Spring MVC to the page:
public ModelAndView get() {
// ...
ModelAndView mav = new ModelAndView();
List<String> options = Arrays.asList(getOptionsFromDatabaseAndConvertToStringList());
mav.addObject("options",options );
mav.setViewName("someview");
return mav;
}
Third
options
now need to be bound in the freemarker template and can then be accessed like any other freemarker variable (i.e. NO quotation marks):
<@spring.bind "options" />
<form action="whatever" method="POST">
<@spring.formSingleSelect "targetBean.choice", options, " " />
<input type="submit" value="submit"/>
</form>
这篇关于下拉框 - 从Spring MVC模型/上下文到使用freemarker形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!