Spring <form:select>不会打印任何内容

时间:2017-10-06 01:20:22

标签: java spring-mvc drop-down-menu

我正在尝试使用弹簧控制器将简单列表值打印到选择框。它使用

成功返回所有值
${test}

但是当我尝试这个时它不起作用。解决方案将不胜感激。

控制器

 @RequestMapping(value="/flight", method = RequestMethod.GET) 
public ModelAndView homePage(CountryCodes country) {
    //model.addAttribute("userName", user.getUserName());
    /*List<CountryCodes> countryCodes = userService.getCountryCodes();
    model.addAttribute("codes", countryCodes);*/

    ModelAndView mv= new ModelAndView("FlightDetails");
    List<String> test = new ArrayList<String>();
    test.add("Human Resources");
    test.add("Finance");
    test.add("Admin");
    test.add("Quality Assurance");
    test.add("Products");

    mv.addObject("test", test);
    return mv;
}

在jsp中选择框

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

1 个答案:

答案 0 :(得分:0)

您是否包含spring:form taglib参考?这种方式对我有用:

控制器:

@RequestMapping(value="/flight", method = RequestMethod.GET) 
    public ModelAndView homePage(CountryCodes country) {
        //model.addAttribute("userName", user.getUserName());
        /*List<CountryCodes> countryCodes = userService.getCountryCodes();
        model.addAttribute("codes", countryCodes);*/

        ModelAndView mv= new ModelAndView("FlightDetails");
        List<String> test = new ArrayList<String>();
        test.add("Human Resources");
        test.add("Finance");
        test.add("Admin");
        test.add("Quality Assurance");
        test.add("Products");

        mv.addObject("test", test);
        mv.addObject("country", country);
        return mv;
    }

JSP:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
        <head>
            <title>Select options from controller</title>
        </head>
        <body>
            <h1>Select options from controller</h1>
            ${test}
            <form:select path="country" class="form-control"> 
                <form:options items="${test}"  class="form-control"/> 
            </form:select>
        </body>

    </html>