webtier@glassfish.java.net

Re: [webtier] Re: getting object in h:selectOneMenu and f:selectItems

From: Joel Weight <digitaljoel_at_gmail.com>
Date: Thu, 22 Jul 2010 09:42:50 -0600

For a more user friendly view of your MyClass objects, you should provide a
custom converter for that type that will take care of converting from a
friendly string representation to the object. I wrote a blog about a very
simple converter in JSF 2 here
http://digitaljoel.wordpress.com/2010/01/11/jsf-2-custom-converter/ but if
you don't want to read through that, here's the source of the converter I
wrote for Google App Engine's Key type

package jota.soc.ui.converter;
02
03import com.google.appengine.api.datastore.Key;
04import com.google.appengine.api.datastore.KeyFactory;
05import javax.faces.component.UIComponent;
06import javax.faces.context.FacesContext;
07import javax.faces.convert.Converter;
08import javax.faces.convert.FacesConverter;
09
10/**
11 * Converter for Google Key.
12 * @author Joel.Weight
13 */
14_at_FacesConverter( value="keyConverter" )
15public class KeyConverter implements Converter {
16
17 /**
18 * converts the String representation of the key back to the Object
19 */
20 public Object getAsObject( FacesContext context, UIComponent
component,
21 String value )
22 {
23 // will throw new IllegalArgumentException if it can't parse.
24 return KeyFactory.stringToKey( value );
25 }
26
27 /**
28 * converts the Key object into its String representation.
29 */
30 public String getAsString( FacesContext context, UIComponent
component,
31 Object value )
32 {
33 if ( value instanceof Key )
34 {
35 return KeyFactory.keyToString( (Key)value );
36 }
37 else
38 {
39 throw new IllegalArgumentException( "Cannot convert non-key
object in KeyConverter" );
40 }
41 }
42}

Joel

On Thu, Jul 22, 2010 at 8:28 AM, <webtier_at_javadesktop.org> wrote:

> PLEASE HELP!
>
> I can't find any solution for this problem!
> [Message sent by forum member 'andy85']
>
> http://forums.java.net/jive/thread.jspa?messageID=478277
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: webtier-help_at_glassfish.dev.java.net
>
>