Seems I spoke too soon. Although the list of countries displays correctly, it never gets saved.
I have a CountryEditor that has the methods required.
public class CountryEditor extends PropertyEditorSupport {
private GenericManager<Country, Long> countryManager = null; public void setCountryManager(GenericManager<Country, Long> countryManager) { this.countryManager = countryManager; } /**
   * (non-Javadoc)
   *
   * @see java.beans.PropertyEditor#setAsText(java.lang.String)
   */
   @Override
   public void setAsText(String id) throws IllegalArgumentException {
       Country country = countryManager.get(Long.valueOf(id));
       setValue(country);
   }
@Override
   public String getAsText(){
       Country country = (Country) getValue();
       return String.valueOf(country.getId());
   }

}

I'm debugging my app, with break points in both methods and these two methods never get called, even though the thread goes through the initBinder method. The country manager gets set by a bean property in dispatcher-servlet.xml.

WebsiteFormController snippet:

protected Map referenceData(HttpServletRequest request) throws Exception {
       Map retval = new HashMap<String, Object>();
       retval.put("countries", countryManager.getAll());
       return retval;
}

@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { binder.registerCustomEditor(Country.class, "country", new CountryEditor());
   }

websiteform.jsp snippet:
<spring:bind path="website.contact.country">
<SELECT size="1" name='<c:out value="${status.expression}" />1'>
                       <c:forEach var="country" items="${countries}">
                           <OPTION value='<c:out value="${country.id}" />'
<c:if test="${website.contact.country.id==country.id}">SELECTED</c:if>>
                           <c:out value="${country.name}"/> </OPTION>
                       </c:forEach>
                   </SELECT>
               </spring:bind>

dispatcher-servlet.xml snippet:
<bean id="countryEditor" class="myapp.model.customeditor.CountryEditor">
       <property name="countryManager" ref="countryManager"/>
   </bean>

Cheers
Aled
Aled Rhys Jones wrote:
Nice one Mike, finally got it working after spending most of yesterday scratching my head and cursing google for not finding exactly what I wanted ;-)

Cheers
Aled
Michael Horwitz wrote:
You are going to need to provide Spring MVC with a way to get from an id to a country and vice-versa. As you correctly point out this is done by registering a custom property editor in your controller. To do this you need to:

1) Extend the class PropertyEditorSupport to create the property editor: http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PropertyEditorSupport.html <http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PropertyEditorSupport.html>

You need to override the getAsText() and setAsText() methods. The first should call getValue() and return the id as text, and the second should accept a text id and call setValue() on the editor with the corresponding country object.

2) You then register this custom editor using one of the appropriate methods on your controller: initBinder() see api on http://tinyurl.com/2sutrm

And it should all work like magic....

Mike.

On 7/1/07, *Aled Rhys Jones* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    Hi peeps

Could someone recommend the best way of populating a drop down list in edit/add forms? New to spring mvc and not sure of the correct way of
    going about it.

    I have a list of countries stored in the database.  Access to
    these are
provided by a Generic Manager for crud. One of my edit pages needs to
    associate the object with a Country.  I've tried to do it with
    referenceData but I'm getting problems.

    protected Map referenceData(HttpServletRequest request) throws
    Exception {
            Map retval = new HashMap<String, Object>();
            retval.put("countryList", countryManager.getAll());
            return retval;
    }

    The above does return a list of country objects.

    I've got the below in my jsp:
    <form:select path="contact.country">
        <form:option value="-" label="--Please Select"/>
        <form:options items="${countryList}" itemValue="id"
    itemLabel="name"/>
    </form:select>

    Unfortunately this throws a JasperException when I try to load the
    page.  If I remove the above from the jsp the page works fine.
    From my googling, it seems I may need to use a PropertyEditor, but I
can't find any concrete examples of what I'm trying to do, and how to
    use the PropertyEditor in this context.

    Methinks I really need to buy a spring mvc book ;-)

    Any ideas?

    Thanks
    Aled

---------------------------------------------------------------------
    To unsubscribe, e-mail: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    For additional commands, e-mail: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>


------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition. Version: 7.5.476 / Virus Database: 269.9.14/882 - Release Date: 30/06/2007 15:10

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to