【发布时间】:2018-02-26 11:47:37
【问题描述】:
我正在尝试将列表绑定到 JSP 中的下拉列表。下面是我的控制器和 JSP。
控制器:
@Controller
public class WeatherServiceController {
@Value("#{'${countryList}'.split(',')}")
private List<String> countries;
@ModelAttribute("CountriesList")
private List<String> getCountries(){
System.out.println(countries.size());
return countries;
}
@RequestMapping(value = "/getweather", method=RequestMethod.GET)
public ModelAndView getWeather(){
Place p = new Place();
ModelAndView mav = new ModelAndView();
mav.addObject("place",p);
mav.setViewName("home");
return mav;
}
}
JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Weather Service Client - Home</title>
</head>
<body>
<h2>Welcome to Weather Service</h2>
<form:form modelAttribute="place" action="getWeather">
<table>
<tr>
<td><form:label path="country">Country:</form:label></td>
<td>
<form:select path="country" items="${CountriesList}">
</form:select>
</td>
</tr>
</table>
</form:form>
</body>
</html>
但我收到类似“类型 [java.lang.String] 对选项项无效”的错误。国家/地区列表没有出现在 jsp 页面中。请帮助我在这里做错了什么。
【问题讨论】:
-
很可能 items="${CountriesList}" 找不到您的列表。使用 WeatherServiceController 的名称作为前缀
标签: spring jsp spring-mvc el