Spring MVC表格选择选项

时间:2015-10-27 03:11:22

标签: spring jsp model-view-controller

简单的弹簧MVC应用程序。我想填充一个表单:在我的JSP中选择一个填充在Controller中的地图。

在我的JSP中,表单:select不带任何选项。如果我从chrome检查HTML元素,则不会有任何选项。在我的项目的其他地方,同样的方法有效,但在这种情况下它是一个枚举。

简要代码摘要如下:

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

Map在构造函数中初始化。

在控制器

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);

在我的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>

对于同一个类,正确显示其他字符串属性。问题仅适用于选择选项。相同的代码片段在我使用ENUM的另一个屏幕中正常工作。

2 个答案:

答案 0 :(得分:0)

你可以尝试一下吗?这可能有效。

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

设置映射,然后使用映射对象填充options标记。

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

答案 1 :(得分:0)

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>