Dropdown box - from Spring MVC model / context to form using freemarker

不问归期 提交于 2019-12-30 09:47:07

问题


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!


回答1:


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>



回答2:


According to the docs for formSingleSelect:

@param options a map (value=label) of all the available options

I think your best bet is to provide a map with the values as keys and labels as map values. Otherwise freemarker will probably try to do that wrapping for you and you have less control.

I have just validated: if you supply your own map everything is just fine.

<spring.formSingleSelect "target.property" referenceToYourMapProvidedInController ""/>


来源:https://stackoverflow.com/questions/11433510/dropdown-box-from-spring-mvc-model-context-to-form-using-freemarker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!