2017-10-06 72 views
0

我想使用彈簧控制器將簡單列表值打印到選擇框。它成功返回所有值使用彈簧<form:select>不打印任何東西

${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> 

回答

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>