Spring下拉不渲染选项

时间:2012-10-08 15:58:09

标签: spring spring-mvc

在一个简单的Spring 3 MVC应用程序中,我有一些没有显示任何选项的下拉菜单。

jsp表单摘要是:

<form:form method="POST" commandName="carSelection">
        <form:errors path="*" cssClass="errorblock" element="div" />
        <table>
            <tr>
                <td>Car Make :</td>
                <td><form:select path="makes" multiple="false">
                      <form:options items="${makes}"/>
                    </form:select>
                                </td>
                <td><form:errors path="makes" cssClass="error" /></td>
            </tr>

            <tr>
                <td>Car Make (SimpleList):</td>
                <td><form:select path="simpleModels" multiple="false">
                      <form:options items="${simpleModels}"/>
                    </form:select>
                                </td>
                <td><form:errors path="makes" cssClass="error" /></td>
            </tr>
            <tr>
                <td>Car Model :</td>
                <td>
                    <form:select path="models" items="${models}" multiple="false">
                        <form:option value="NONE" label="--- Select ---" />
                        <form:options items="${models}" itemValue="modelId" itemLabel="modelName"/>
                    </form:select>
                </td>
                <td><form:errors path="models" cssClass="error" /></td>
            </tr>

        </table>
    </form:form>

Controller类我有以下内容:

@Controller
@RequestMapping("/car")
public class CarController {

    /**
     * Initialise the car selection form
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/carSelectionForm")
    public ModelAndView loginForm() throws Exception {
        ModelAndView mav = new ModelAndView();
        CarSelection cs = new CarSelection();
        List<CarMake> makes = new ArrayList<CarMake>(5);
        makes.add(new CarMake(1, "Ford"));
        makes.add(new CarMake(2, "Fiat"));
        makes.add(new CarMake(3, "Renualt"));
        makes.add(new CarMake(4, "Kia"));
        makes.add(new CarMake(5, "Ferrari"));
        cs.setMakes(makes);

        List<String> simpleList = new ArrayList<String>(5);
        simpleList.add("Ford");
        simpleList.add("Fiat");
        simpleList.add("Renualt");
        simpleList.add("Kia");
        simpleList.add("Ferrari");
        cs.setSimpleModels(simpleList);

        mav.setViewName("carSelectionForm");
        mav.addObject("carSelection", cs);
        return mav;
    }   
}

'NONE'选项会渲染,是唯一显示的选项。我错过了一些基本的东西吗?

1 个答案:

答案 0 :(得分:1)

您需要在options属性items中设置正确的列表。

<form:select path="makes" multiple="false">
   <form:options items="${carSelection.makes}"/>
</form:select>


<form:select path="simpleModels" multiple="false">
  <form:options items="${carSelection.simpleModels}"/>
</form:select>

但我没有控制器中的3.下拉项目