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

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]> 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]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to