Announcement Announcement Module
Collapse
No announcement yet.
Problem using WebBindingInitializer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem using WebBindingInitializer

    Hello everybody,

    I'm using annotated controller, everything works as expected. I have a WebBindingInitializer which register a few custom propertyEditor.

    I have a form with a listbox of countries filled with a @ModelAttribute method :
    Code:
    @ModelAttribute("countries")
    public List<Country> populateCountries () {
       List<Country> countries = countryService.getAllCountry();
       return countries;
    }
    The list is filled without problem, but when the jsp should be displayed, the CountryEditor (custom propertyEditor) is called for every country in the list !
    And the jsp is displayed after a few seconds because of the calls to the database in the CountryEditor.

    Code:
    public class CountryEditor extends PropertyEditorSupport {
    
    	Logger logger = Logger.getLogger(CountryEditor.class);
    
    	private final CountryService countryService;
    
    	/**
    	 * @param countryService
    	 */
    	public CountryEditor(CountryService countryService) {
    		this.countryService = countryService;
    	}
    
    	@Override
    	public void setAsText(String text) throws IllegalArgumentException {
    		String countryId = text;
    		logger.debug("retrieval of Country with Id = " + countryId);
    		Country country = countryService.getCountryById(countryId);
    		setValue(country);
    	}
    
    
    }
    The JSP :
    Code:
    <form:form method="post" modelAttribute="company" >
        <form:errors path="*" cssClass="errors" />
        <fieldset>
            <p>
                <label for="country"><spring:message code="company.label.country" /></label>
                <form:select path="country" id="country" >
                        <form:options items="${countries}" itemLabel="label" itemValue="id"  />
                </form:select>
            </p>
    
        </fieldset>
        <button type="submit" ><spring:message code="company.button" /></button>
    
    </form:form>
    I thought the setAsText method was only used when the form is submited.

    What did I wrong ?

    Thanks
    Last edited by jybosse; Dec 3rd, 2008, 08:38 AM. Reason: added jsp code

  • #2
    Ok, I found something, it seems that it is the select tag that calls the CountryEditor.
    I found a call in the abstract class
    Code:
    org.springframework.web.servlet.tags.form.SelectedValueComparator
    Does someone knows how it works ?

    Thanks

    Comment


    • #3
      I've found a lot of thread about this problem and no real answer...

      I've found that, in the class SelectedValueComparator, there is a special condition : if the candidate is a String

      the code (the condition is the last one) :
      Code:
      ...
      else if (editor != null && candidate instanceof String) {
      // Try PE-based comparison (PE should *not* be allowed to escape creating thread)
      	String candidateAsString = (String) candidate;
      	Object candidateAsValue = null;
      	if (convertedValueCache != null && convertedValueCache.containsKey(editor)) { candidateAsValue = convertedValueCache.get(editor);
      	}
      	else {
      	           editor.setAsText(candidateAsString);
      		   candidateAsValue = editor.getValue();
      		   if (convertedValueCache != null) {
      			convertedValueCache.put(editor, candidateAsValue);
      		   }
      	}
      	if (ObjectUtils.nullSafeEquals(boundValue, candidateAsValue)) {
      		return true;
      	}
      }
      ...
      I don't understand this special condition ? why is it different if the value for the label/value pair is an Integer or something else ?

      Because when it is a String, there is a call to the setAsText method of the editor for each line in the given collection of objects ! in my cases a lot of select query to the DB !

      Comment


      • #4
        Is there no spring guru who can help me ?

        Comment


        • #5
          The problem appears too in the petclinic sample, the only thing you need to do to get the behavior is to set the value used to identify the PetType as a String, and the PetTypeEditor will go to the database get ALL PetType for every PetType in the list when the form:select is rendered !!

          If you set the value as an Integer or Long or something else, you don't have the problem.

          I can't believe someone didn't have to face this problem...

          Comment


          • #6
            I've found a thread in this forum which says that we must not use ItemValue in a select tag when using a propertyEditor : http://forum.springframework.org/showthread.php?t=35575

            That solution works !

            Can somebody tell me why ?

            Comment

            Working...
            X