Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 350
Pete Avatar asked Jul 11 '12 13:07

Pete


2 Answers

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>
like image 68
Pete Avatar answered Sep 29 '22 04:09

Pete


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 ""/>
like image 26
Cristian Botiza Avatar answered Sep 29 '22 04:09

Cristian Botiza