You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Randahl Fink Isaksen <ra...@rockit.dk> on 2006/10/16 16:07:33 UTC

Identifiers and custom UI components of singleton nature

I wrote a UIComponent which handles my style sheets. This component is 
meant to only be placed in one single place in the component tree, and 
it is meant to always reside directly under the component tree root. To 
make it easy to locate the component, I thought I would enforce it to 
always have the same id, so I wrote this code (which does not work like 
I expected it to):

public class StylesComponent extends UIComponentBase {
    private static final String ID = "styles";
    public StylesComponent() {
        setId(ID);
    }
}

That way I was hoping that people could use my component like this
     <styles/>
and automatically get a component with the id "styles". This is not the 
case, however, since whenever I do not EXPLICITLY assign an id in the 
tag, JSF seems to insist on creating one for me. So even though I wrote 
the line "setId(ID);" I still get an automatic id. The hack I am using 
at the moment is this:
    <styles id="styles"/>
Which has the same effect, but forces my users to remember to assign 
this default id which I have chosen.

Can anyone tell me what is wrong or why this is so, and / or how to 
achieve such a proper default identifier in an alternative way?

Thanks
Randahl


RE: Identifiers and custom UI components of singleton nature

Posted by Michael Matz <mm...@kamakuraco.com>.
I haven't tried this but couldn't you override setId?
 
public void setId(String id) { /* ignore */ }

Michael


-----Original Message-----
From: Randahl Fink Isaksen [mailto:randahl@rockit.dk] 
Sent: Monday, October 16, 2006 4:08 AM
To: MyFaces Discussion
Subject: Identifiers and custom UI components of singleton nature

I wrote a UIComponent which handles my style sheets. This component is 
meant to only be placed in one single place in the component tree, and 
it is meant to always reside directly under the component tree root. To 
make it easy to locate the component, I thought I would enforce it to 
always have the same id, so I wrote this code (which does not work like 
I expected it to):

public class StylesComponent extends UIComponentBase {
    private static final String ID = "styles";
    public StylesComponent() {
        setId(ID);
    }
}

That way I was hoping that people could use my component like this
     <styles/>
and automatically get a component with the id "styles". This is not the 
case, however, since whenever I do not EXPLICITLY assign an id in the 
tag, JSF seems to insist on creating one for me. So even though I wrote 
the line "setId(ID);" I still get an automatic id. The hack I am using 
at the moment is this:
    <styles id="styles"/>
Which has the same effect, but forces my users to remember to assign 
this default id which I have chosen.

Can anyone tell me what is wrong or why this is so, and / or how to 
achieve such a proper default identifier in an alternative way?

Thanks
Randahl