package org.apache.struts2.showcase.conversion public class EnumTypeConverter extends StrutsTypeConverter { //old methods..
//new method to convert map contents to Strings.. @Override public String[] convertToStrings(Map context, Object o) { int j=0; Strings[] s; List l = (List) o; String result ="<"; for (Iterator i = l.iterator(); i.hasNext(); ) { s[j++]=i.next(); } return s; } //...other methods.. } FWIW Martin-- ----- Original Message ----- From: "Jeromy Evans" <[EMAIL PROTECTED]> To: "Struts Users Mailing List" <user@struts.apache.org> Sent: Tuesday, February 19, 2008 5:23 PM Subject: Re: Type conversion questions > Daniel Baldes wrote: > > Hello, > > > > I have a web form where you can, generally speaking, assign objects to > > groups. This is done via a multiple select box which displays an > > object's name and uses its ID property as value. > > > > So, usually, my action would get back a String array containing the > > IDs of the selected objects. I want the framework to load the > > corresponding objects from the database and pass them as a Collection > > to my action's setObjects(Collection<Object> objects) method, so that > > my action doesn't have to deal with loading objects. > > > > So I wrote a type converter. The "convertFromString" method takes the > > String array with the IDs, determines the target element type using > > ObjectTypeDeterminer, loads the objects and returns them in a > > collection. In this direction - form to action - it works quite well. > > > > Great. I've never tried this myself. I suppose you run the risk of > some performance penalties doing it here (eg. select n+1 due to > iteration) and the conversion error exception is probably not ideal, but > otherwise I'm impressed. > > Now my questions: > > > > 1. extending StrutsTypeConverter forces me to implement String > > convertToString(Map context, Object o). I think there is no common > > sense way of converting a collection back to a String in this context. > > It would, maybe, make sense to convert it to a String[]. But what I > > actually want is to have access to the unconverted collection in the > > JSP. This might seem strange from some point of view, and I could live > > without it, but is there a way to do this? Example: > > > > <s:select multiple="true" > > name="myObjects" > > list="myObjects" listKey="id" listValue="name" /> > > > > This way I could use getMyObjects() for displaying my objects and > > setMyObjects() for setting them (using only their IDs). > > > > It does really follow the intended contract of the interface, but I > don't see why convertFromString can't return a different view of the > object than convertToString. Have you tried it? This effectively gives > you the "databinding" feature of ASP.net (which I personally cringe at, > but can't argue with how productive it is to work with) > > > > 2. Is there a way to define a type converter application wide, like > > "always use this type converter for setting types which are subclasses > > of X" and "for setting types which are Collection<subclass-of-X>"? > > This way I could define the type converter once for all > > persistence-capable classes. > > > classpath:xwork-converters.properties allows you to define global > converters by class name. You may be able to the the element_ feature > for collections. > Otherwise visitor validation will have to do > > --------------------------------------------------------------------- > 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]