【问题标题】:JSP : How to link form select option to Java objectJSP:如何将表单选择选项链接到 Java 对象
【发布时间】:2015-11-30 13:02:47
【问题描述】:

我有两个链接的对象。 Address 有一个 Country 和一个由 countryId 链接的 ManyToOne 关系。我正在尝试使用 JSP 添加网页以添加新的Address。在此页面上,您可以从下拉列表中选择 Country

我尝试了多种不同的方法来做到这一点,但我似乎无法将下拉列表中的 Country 对象链接到 Address 中的 Country 对象,我不断收到以下错误

Field error in object 'address' on field 'country': rejected value [com.company.project.domain.Country@670b11d7]; codes [typeMismatch.address.country,typeMismatch.country,typeMismatch.com.company.project.domain.Country,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [address.country,country]; arguments []; default message [country]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.company.project.domain.Country' for property 'country'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.company.project.domain.Country] for property 'country': no matching editors or conversion strategy found

这是我尝试过的许多不同选项中的两个,但我一直收到相同的错误

试试 1 JAVA

List<Country> countries = countryService.getAllCountrys();
    LinkedHashMap<Country, String> countryMap = new LinkedHashMap<Country, String>();
    for(Country country : countries){
        countryMap.put(country, country.getCountryName());
    }

    model.addAttribute("countries", countryMap);

试试 1 JSP

<tr>
    <td>Country:</td>
    <td><form:select path="country">
    <form:options items="${countries}" />
    </form:select>
    </td>
</tr>

试试 2 JAVA

List<Country> countries= countryService.getAllCountry();
    model.addAttribute("countries", countries);

试试 2 JSP

<tr>
    <td>Country:</td>
    <td><form:select path="country" id="country">
        <form:options items="${countries}" itemValue="country" itemLabel="countryName" />
    </form:select>
</tr>

我可以正确填充下拉列表,但我尝试保存它似乎是在尝试保存字符串值而不是对象值。我正在使用 POST 请求来保存页面

有人知道怎么做吗?

【问题讨论】:

    标签: javascript java jquery spring jsp


    【解决方案1】:

    对于这些情况,我就是这样做的:

    JAVA

    List<Country> countries= countryService.getAllCountry();
    model.addAttribute("countries", countries);
    

    JSP

    <select name="idCountry">   
    <c:forEach items="${countries}" var="country">
    <option value="${country.id}">${contry.countryName}</option> 
    </c:forEach>
    

    发布 JAVA

    Country c = CountryRepositroy.findById();
    Adresse.setCountry(c);
    

    【讨论】:

      【解决方案2】:

      您不会得到对象值,因为 HTML 表单提交了由 form:options 的 itemValue 属性分配给下拉列表的字符串值。 您尝试的一件事是将 id 作为国家/地区的值并从中创建国家/地区对象,然后尝试保存它。

      此外,您还提交了整个 Country 对象,我建议您将 id 作为值和 name 作为值显示在下拉列表中,并在服务器端从 id 创建国家对象。

      【讨论】:

        【解决方案3】:

        您需要将您的收藏添加到请求中。请参阅下面的示例

            request.setAttribute("countries", countryMap);
            RequestDispatcher view = request.getRequestDispatcher("Index.jsp");
            view.forward(request, response);
        

        【讨论】:

          猜你喜欢
          • 2012-01-11
          • 1970-01-01
          • 1970-01-01
          • 2017-01-28
          • 2010-09-13
          • 1970-01-01
          • 2012-09-15
          • 1970-01-01
          • 2020-02-17
          相关资源
          最近更新 更多