Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 convert doesnt work between 2 different types Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 convert doesnt work between 2 different types

    I had a problem with countries (in hibernate) and its translations in combination with sorting them. I had 2 options; First one, get a plain list and translate them one by one, then sort them and return them.

    The second one was better in my opinion. It is a @Entity of a view, that returns the same fields as the Countries table, except with an added language_id and a translation, on which it can sort.

    I have an Address object, which holds a Country. This Address object has an edit form, which has a <form:select> element with all the countries in it.

    The problem: in the JSP it wont convert the TranslatedCountry object to its ID, while there IS a converter for it. My first guess is because its not a Country object, so the converter service doesnt know which converter to choose.

    Let me back this up with some code

    Code:
    <form:select path="address.country">
        <c:forEach items="${countries}" var="country">
            <form:option value="${country}">
                ${x:html(country.translation)}
            </form:option>
        </c:forEach>
    </form:select>
    Code:
    public class TranslatedCountryToString implements Converter<TranslatedCountry, String> {
    
    	@Override
    	public String convert(TranslatedCountry country) {
    		return "" + country.getId();
    	}
    
    }
    Code:
    public class CountryToString implements Converter<Country, String> {
    
    	@Override
    	public String convert(Country country) {
    		return "" + country.getId();
    	}
    
    }
    The Country and TranslatedCountry objects are not special, so I won't bother posting them unless requested.

    The resulting HTML is:
    HTML Code:
    <select name="address.country" id="address.country">
      <option value="nl.x.model.TranslatedCountry@113cf51">Afghanistan</option>
      <option value="nl.x.model.TranslatedCountry@19006c9">Ňland</option>
      <option value="nl.x.model.TranslatedCountry@ad8e84">AlbaniŽ</option>
      .. etc
    Oh, and the item in spring-dispatcher-servlet.xml

    HTML Code:
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
      <property name="converters">
        <list>
          <bean class="nl.x.view.convert.TranslatedCountryToString" />
    				
          <bean class="nl.x.view.convert.StringToCountry" />
          <bean class="nl.x.view.convert.CountryToString" />
    My question summarized: how do I get Spring to convert the TranslatedCountry object with the corresponding converter? Is it even possible if the field it is bound to is not of the same type (Address.country is not a TranslatedCountry type)?

  • #2
    You might try this approach instead.

    Code:
    <form:select path="address.country">
      <form:options items="${countries}" itemLabel="country.translation" itemValue="country.id"/>
    </form:select>

    Comment


    • #3
      Although i tried this before and thought it would have the same problem, I investigated this case a bit further and came to the conclusion it could work, only if the .equals method is overridden on the Country object so that it also compares to a TranslatedCountry. I have done this now and it appears to be working, so thanks for the answer.

      Comment

      Working...
      X