views:

148

answers:

1

I'm trying to bind multiselect to my object. The form is created and the post works and no errors occur when I don't select anything. If I select a language the object gets language value but the form breaks.

        HashMap<String, String> languages = new LinkedHashMap<String, String>();
    languages.put("eng","english");
    languages.put("ger","german"); 

    session.setAttribute("languages", languages);

<@spring.formMultiSelect "advancedSearch.languages", languages, " "/>

AdvancedSearch class has

String languages;

and it gets a value "eng, rus" but the form doesn`t work after it gets a value.

Error message

HTTP ERROR 500

Problem accessing /opac/advSearch. Reason:

Expected collection or sequence. list evaluated instead to

freemarker.template.SimpleScalar on line 368, column 12 in spring.ftl. Caused by:

Expected collection or sequence. list evaluated instead to freemarker.template.SimpleScalar on line 368, column 12 in spring.ftl. The

problematic instruction:

==> list list as nextInList [on line 368, column 5 in spring.ftl] in user-directive spring.formMultiSelect [on line 59, column 41 in advancedSearchView.ftl]

I think the problem is with the way I save languages. Right now it`s String. I guess I need to hold it some other way. I tried String[] List but can't get things to work.

A: 
<@spring.bind "advancedSearch.languages"/>
<#assign selectedLangs = spring.status.value?default(" ")>
<select multiple="multiple" id="${spring.status.expression}" name="${spring.status.expression}"}>
    <#list languages?keys as value>
    <#if selectedLangs?contains(value) >
        <#assign isSelected = true>
    <#else>
        <#assign isSelected = false>
    </#if>
    <option value="${value?html}"<#if isSelected> selected="selected"</#if>>${languages[value]?html}
    </#list>
</select>

This worked!

ivar