There are two parts to making something like this work with Spring MVC.
First off you need to pass a list of objects to the form which get rendered
as the select list. Using the <form:select ...> tag you pass in the name of
a collection and the properties on the items in the collection to use as the
label and value for each select option. I assume you have this part working?
If the form backing object's dataVendor field is already populated, then
Spring needs to work out which of the items in the list is already selected.
This is where the custom editor comes in - it takes the DataVendor in the
form backing object, converts it to a string and compares it to the values
in the select list. Whichever value matches is marked as being "selected".

The next part is the user selecting an item in the list, and Spring using
the selection to populate a field on your form backing object. I am assuming
that in your case name is the label and vendorID is the value. So if the
user selected "Vendor Foo" from your select list, then the value "vendor-10"
would be passed back to the controller. Spring needs to know how to turn
this in to DataVendor object, which is where your custom editor comes in.

The gist of the above is that your custom editor is always working with a
single DataVendor object, and never with the list.

Mike.

2008/5/7 Shah, Chirag M (Contractor) <[EMAIL PROTECTED]>:

>  Hello,
>
> I was going through your excellent article at
> http://appfuse.org/display/APF/Using+Spring+MVC#UsingSpringMVC-controllerform
> I am great fan of appfuse.
>
> I do have one question though in context of SPRING framework.
> I would be grateful if you could take a moment from your busy  schedule
> and answer it.
>
> I am having *formBackingObject* with ArrayList as one of the fileds
> with objects of one POJO like
>
> public class DataVendor implements Serializable
> {
>  public String vendorID;
>  public String name;
> }
>
> On JSP I am going to iterate over this arraylist with vendorID as *value*and 
> name as
> *label* to prepare my multi select dropdown box.
> I am using SPRING 2.5.2 and <form:select> tag.
>
> This forces me to create a CustomEditor for converting the string back
> into object and register it thourgh.
> binder.registerCustomEditor(ArrayList.class, new MyCustomEditor());
> The only 2 methods that I implemented are as follows.
> It is not working because of ClassCastException.
>
> Please help me out. I really need you at this point of time.
>
>
>
>  public String getAsText()
>  {
>     ArrayList al = (ArrayList) getValue();     -------> exception occurs
> here because the getValue returns DataVendor.java
>    for(int i= 0; i < al.size(); i++)
>    {
>         DataVendor dv = (DataVendor)al.get(i);
>         s = s + dv.getVendorID();
>     }
>    return s;
>  }
>
>
>  public void setAsText(final String text)
>  {
>    ArrayList newal = new ArrayList();
>    ArrayList dataVendor = (ArrayList)ctx.getAttribute("DATA_VENDOR");
> ----> coming from ServletContext
>
>    StringTokenizer st = new StringTokenizer(text, ",");
>    while(st.hasMoreTokens() == true)
>    {
>        String s = st.nextToken();
>       for (int i=0; i < dataVendor.size(); i++)
>      {
>          DataVendor dv = (DataVendor)dataVendor.get(i);
>         if(dv.getVendorID().equalsIgnoreCase(s) == true)
>        {
>            newal.add(dv);
>      }
>     }
>    }
>   setValue(newal);
>  }
>
>
>
> Thanks.
> Chirag
>
>
> This e-mail and its attachments are confidential and solely for the
> intended addressee(s). Do not share or use them without Fannie Mae's
> approval. If received in error, contact the sender and delete them.
>
>
>
> This e-mail and its attachments are confidential and solely for the
> intended addressee(s). Do not share or use them without Fannie Mae's
> approval. If received in error, contact the sender and delete them.
>
>

Reply via email to