Skip to main content

how do you write a custom UIComponent that accepts a custom attribute type

4 replies [Last post]
tedman
Offline
Joined: 2004-06-18

Hi I'm trying to write a custom UIComponent which accepts attributes which are of a non-native type.
I've already written custom UIComponents which use String and ints as attributes but now I want a custom class as an attribute.

i.e. in my Jsf I want to put something like

I already know how to do this for String and int and other built-in primitive types so I'm specifically asking about Custom classes only.

So what I've tried is something like this (which doesn't work) :

public class MyOwnObject
{
}

@ManagedBean
public class MyJsfBean
{
public MyOwnObject getMyOwnObject() ...
}

@FacesComponent(value="renderMyObject")
public class RenderMyObject extends UIComponentBase
{
private MyOwnObject myOwnObject=null;

public void setMyOwnObject(MyOwnObject x) ...
}

In the above example, the getMyOwnObject() is never even called in the JSF Managed Bean, and the setMyOwnObject is never called in the component. I'm thinking there's something special about custom objects which I can't seem to find any documentation about... If I switch the above to String instead of my custom object it all works.

Anyone know how I'm suppose to properly do the above? Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Jason Lee

I think you're going to need a Converter for your Object. JSF has to be
told how to convert the object to a String to display on the client, as
well as how to convert that String back to the object in question.

On 1/7/10 5:08 PM, webtier@javadesktop.org wrote:
> Hi I'm trying to write a custom UIComponent which accepts attributes which are of a non-native type.
> I've already written custom UIComponents which use String and ints as attributes but now I want a
>
> i.e. in my Jsf I want to put something like
>
>
>
> I already know how to do this for String and int and other built-in primitive types so I'm specifically asking about Custom classes only.
>
> So what I've tried is something like this (which doesn't work) :
>
> public class MyOwnObject
> {
> }
>
>
> @ManagedBean
> public class MyJsfBean
> {
> public MyOwnObject getMyOwnObject() ...
> }
>
> @FacesComponent(value="renderMyObject")
> public class RenderMyObject extends UIComponentBase
> {
> private MyOwnObject myOwnObject=null;
>
> public void setMyOwnObject(MyOwnObject x) ...
> }
>
> In the above example, the getMyOwnObject() is never even called in the JSF Managed Bean, and the setMyOwnObject is never called in the component. I'm thinking there's something special about custom objects which I can't seem to find any documentation about... If I switch the above to String instead of my custom object it all works.
>
> Anyone know how I'm suppose to properly do the above? Thanks.
> [Message sent by forum member 'tedman' (tedman@sfu.ca)]
>
> http://forums.java.net/jive/thread.jspa?messageID=379456
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>

--
Jason Lee
Senior Java Developer
GlassFish Administration Console

Sun Microsystems, Inc.
Phone x31197/+1 405-343-1964
Email jasondlee@sun.com
Blog http://blogs.sun.com/jasondlee
Blog http://blogs.steeplesoft.com

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

tedman
Offline
Joined: 2004-06-18

Although a converter would in theory work, I'm trying to write a custom component, not a custom converter.

I see no real reason why I shouldn't be allowed to do this as a custom component since it appears to fit the model of a component... This Component is logic intensive and isn't simply just an html rendition of what is passed in. It does logic and processing based on what is passed in.

For now I've simply just put a getFoo() method is my ManagedBean and used to render the html but that's just another hack too.

ashok_sharma
Offline
Joined: 2010-01-10

Hi tedman,

in this case you need to get Value by using ValueBinding technique.
e.g.

ValueBinding vb = getValueBinding("myOutPutObject");
return vb != null ? (Object JavaDoc)vb.getValue(getFacesContext()) ;

Regards
Ashok Sharma
Java Developer

tedman
Offline
Joined: 2004-06-18

cool thanks, that worked.

I tried it before but I couldn't get it working, probably because I assumed the getMyObject() method would be called automatically so when I saw it not being called I assumed it was not working. As it turns out it's not called until some one calls getValueBinding on it.

only minor hickup now is that ValueBinding is deprecated, yet it's replacement ValueExpression is not present in the jars... I guess that's what I get for using pre-release versions.