0
votes

Simple spring MVC application. I want to populate a form:select in my JSP with a map populated in my Controller.

In my JSP, the form:select is not coming with any options. If I inspect the HTML element from chrome, no options are coming. Elsewhere in my project, the same approach works, but in that case it is an enum.

Brief code snippets follow:

//        Domain Class          
public class Expense {
      String attributeSelectedFromJSP;
      Map<String,String> attributesToBeShownInJSP;
      ...
}

Map is initialised in constructor.

In Controller

Expense exp = new Expense();
    // List allEcs is a list of String
    for (ExpenseCategory e : allEcs)    {
        exp.attributesToBeShownInJSP.put(e.toString(), e.toString());
    }
    modelMap.addAttribute(
            "expense", exp);
    return new ModelAndView("addExp.jsp",
            "command", exp);

In my JSP

<form:form method="POST" action="/addNewExpense"
                modelAttribute="expense">
                <div class="form-group">
                    <table >
                    <tr>                    
                        <td>
                            <form:label path="attributeSelectedFromJSP">Select something: </form:label>
                        </td>
                        <td colspan="2">                    
                            <form:select path="attributeSelectedFromJSP" class="form-control">          
                                 <form:options items="${attributesToBeShownInJSP}" />
                            </form:select>
                        </td>
                    </tr>

For the same class, other string attributes are shown properly. Issue is only with the select options. The same snippet of code works fine in another screen where I used an ENUM.

2
I think you need to create a new map for attributesToBeShownInJSP. Then you should add this to your model, and call from items attribute.Burak Keceli

2 Answers

0
votes

Could you try this? This might be working.

<c:set var="entry" value="${expense.attributesToBeShownInJSP}"></c:set>

Set the mapping, and then fill the options tag with mapped objects.

<form:select path="attributeSelectedFromJSP" class="form-control">          
    <form:options items="${entry}" />
</form:select>
0
votes
Why u declare map (Map<String,String> attributesToBeShownInJSP)  in Bean Expense :

Do this 
1.
In controller, add  : 

 @ModelAttribute("myMap")
    public Map<String,String> refData() { 
        Map<String,String> map ...;

        return map;

    }

2.
in jsp

<form:form method="POST" action="/addNewExpense"
                modelAttribute="expense">

<form:select path="attributeSelectedFromJSP" class="form-control">          
         <form:options items="${myMap}" />
</form:select>

</form:form>