最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSP : How to link form select option to Java object - Stack Overflow

programmeradmin3浏览0评论

I have two objects which are linked. Address which has a Country with a ManyToOne relationship linked by countryId. I am trying to add a webpage using JSP to add a new Address. On this page you are able to select a Country from a dropdown.

I have tried a number of different ways to do this but i cannot seem to link the Country object in the dropdown to the Country object in the Address, i keep getting the following error

Field error in object 'address' on field 'country': rejected value [pany.project.domain.Country@670b11d7]; codes [typeMismatch.address.country,typeMismatch.country,typeMismatchpany.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 'pany.project.domain.Country' for property 'country'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [pany.project.domain.Country] for property 'country': no matching editors or conversion strategy found

These are two of many different options i have tried but i keep getting the same error

Try 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);

Try 1 JSP

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

Try 2 JAVA

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

Try 2 JSP

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

I can get the dropdowns populating correctly but wen i try to save it is seems to be trying to save a string value rather then the object value. I am using a POST request to save the page

Does anyone know how to do this?

I have two objects which are linked. Address which has a Country with a ManyToOne relationship linked by countryId. I am trying to add a webpage using JSP to add a new Address. On this page you are able to select a Country from a dropdown.

I have tried a number of different ways to do this but i cannot seem to link the Country object in the dropdown to the Country object in the Address, i keep getting the following error

Field error in object 'address' on field 'country': rejected value [.pany.project.domain.Country@670b11d7]; codes [typeMismatch.address.country,typeMismatch.country,typeMismatch..pany.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 '.pany.project.domain.Country' for property 'country'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [.pany.project.domain.Country] for property 'country': no matching editors or conversion strategy found

These are two of many different options i have tried but i keep getting the same error

Try 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);

Try 1 JSP

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

Try 2 JAVA

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

Try 2 JSP

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

I can get the dropdowns populating correctly but wen i try to save it is seems to be trying to save a string value rather then the object value. I am using a POST request to save the page

Does anyone know how to do this?

Share Improve this question asked Sep 4, 2015 at 11:24 Hip Hip ArrayHip Hip Array 4,77311 gold badges53 silver badges81 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

for these case it's how I do :

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>

POST JAVA

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

You will not get the object value , as HTML form submits the String value that is assigned to the drop down by itemValue attribute of form:options. One thing you try is take id as value of the country and create country object from it and then try to save that.

Also your submitting the Country object as whole, I would suggest you to just get the id as value and name as value to be displayed in drop down and at server side create the country object from id.

You need to add your collection to request. See exapmple below

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

评论列表(0)

  1. 暂无评论