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

前端 未结 2 1445
广开言路
广开言路 2021-01-13 03:21

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

相关标签:
2条回答
  • 2021-01-13 04:16

    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 ""/>
    
    0 讨论(0)
  • 2021-01-13 04:19

    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>
    
    0 讨论(0)
提交回复
热议问题