【问题标题】:Type [java.lang.String] is not valid for option items类型 [java.lang.String] 对选项项无效
【发布时间】: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}" 找不到您的列表。使用 Wea​​therServiceController 的名称作为前缀

标签: spring jsp spring-mvc el


【解决方案1】:

它现在正在工作。 我在 jsp 页面中添加了以下行。

<%@ page isELIgnored="false" %>  

我认为默认情况下“isELIgnored”设置为 false,所以我之前没有包括在内。包含此页面后是绑定列表结果。

【讨论】:

  • 非常感谢您回答您自己的问题。这对我有帮助:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
相关资源
最近更新 更多