Announcement Announcement Module
No announcement yet.
Problem using WebBindingInitializer Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 :
    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.

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

    What did I wrong ?

    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
    Does someone knows how it works ?



    • #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) :
      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 {
      		   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 !


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


        • #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...


          • #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 :

            That solution works !

            Can somebody tell me why ?