Skip to main content

getting object in h:selectOneMenu and f:selectItems

6 replies [Last post]
andy85
Offline
Joined: 2010-07-14
Points: 0

Hi,

I've got a vector, which contains objects of type MyClass with an attribute name. I put this vector (testVector) in a selectetItems-Tag and configure this Tag to show the attribute name of each object in the selectOneMenu. Now I would like to get an object and no String, when the user select an item in a selectOneMenu. But I get a String like [de.test.beans.MyClass@629814, de.test.beans.MyClass@ee58a1, de.test.beans.MyClass@1fce0aa, ...]!?
My code is the following:

Is it a bug of JSF or could you get only Strings on a selectOneMenu?
For your information: I use Mojarra 2.0.2, RichFaces 3.3.3 and Spring Web Flow 2.1.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rbsrodrigues
Offline
Joined: 2010-07-28
Points: 0

Hi,

I think if you used the "metadata-complete="true"" atributte in faces-config.xml your classpath isn't scanned by JSF. This happened with me. I use spring too and I set this property to disable JSF scan. Than Spring can manage my Beans. But I need to set my other configs (validators, converters, listeners manually in faces-config. This isn't good, but I hadn't time to see this question better.

Cheers.

janderssn
Offline
Joined: 2009-03-11
Points: 0

You will need to implement and register a Converter for the MyClass class. The Converter handles the conversion from server side java object to client side string representation.

Eg.

[code]
@javax.faces.convert.FacesConverter(forClass=MyClass.class)
public MyClassConverter implements javax.faces.convert.Converter {
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return new MyClass(value);
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
return ((MyClass) value).getUniqueName();
}
}
[/code]

Further ensure that the object has proper implementations of hashCode and equals.

andy85
Offline
Joined: 2010-07-14
Points: 0

Thanks for your answer!
But if I use the FacesConverter-Annotation the converter is not registered! Only if I define the converter in the faces-config!? Could you tell me why?

Otherwise my faces-config is empty in cause of the use of spring web flow!

Ryan Lubke

On 7/23/10 1:57 AM, webtier@javadesktop.org wrote:
> Thanks for your answer!
> But if I use the FacesConverter-Annotation the converter is not registered! Only if I define the converter in the faces-config!? Could you tell me why?
> Otherwise my faces-config is empty in cause of the use of spring web flow!
What is the version of your empty faces-config.xm?
> [Message sent by forum member 'andy85']
>
> http://forums.java.net/jive/thread.jspa?messageID=478373
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

andy85
Offline
Joined: 2010-07-14
Points: 0

PLEASE HELP!

I can't find any solution for this problem!

Joel Weight

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@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, 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@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>
[att1.html]