1

I want to add a label with an id and an inputfield with an id to a panelgroup (layout box), which has its own id. I expect the label and the inputfield to have the following ids: (viewid):(formid):(panelgroupid):(own id) But somehow the panelgroupid (fachlich1) isnt passed on to the children.

<form id="viewns_Z7_6PQJDK4JGD09F0I34QTLQH2015_:j_id_3" …>
    <div id="viewns_Z7_6PQJDK4JGD09F0I34QTLQH2015_:j_id_3:fachlich1">
        <label id="viewns_Z7_6PQJDK4JGD09F0I34QTLQH2015_:j_id_3:label" for="viewns_Z7_6PQJDK4JGD09F0I34QTLQH2015_:j_id_3:value">Bla</label>
        <input id="viewns_Z7_6PQJDK4JGD09F0I34QTLQH2015_:j_id_3:value" type="text" value="Blubb34534" name="viewns_Z7_6PQJDK4JGD09F0I34QTLQH2015_:j_id_3:value">
    </div>
</form>

Any clue what I might be doing wrong? :(

Thanks for your help!


Edit:

i want to create my own component which basically consits of a surrounding div + label + inputtext. i extend from the HtmlPanelGroup and override the encodeBegin method, where i set the layout to block, set the id of the panelgroup,add the components, after which i call super.encodeBegin:

public void encodeBeginn(FacesContext context)
{
    setLayout("block");
    setId(getId());

    HtmlOutputLabel label = new HtmlOutputLabel();
    label.setId("label");
    label.setFor("value");
    label.setValue(getLabel());
    getChildren().add(label);

    HtmlInputText text = new HtmlInputText();
    text.setId("value");
    text.setValue(getValue());
    getChildren().add(text);

    super.encodeBeginn(context)
}
lostiniceland
  • 3,729
  • 6
  • 38
  • 50
user1323246
  • 433
  • 2
  • 6
  • 23

3 Answers3

4

Only implementations of NamingContainer interface will prepend its own ID to the ID of their children. As per the javadoc it are the following components:

All Known Implementing Classes:

HtmlDataTable, HtmlForm, UIData, UIForm, UINamingContainer

The HtmlPanelGroup, as backed by <h:panelGroup>, is not among them. In standard JSF, only <h:dataTable> and <h:form> implement it. In standard Facelets, only <ui:repeat> implements it as well (through UINamingContainer).

This should not form any technical problem at all. If you think that your concrete problem (which you didn't tell anything about in your question) is caused by this, then you've likely found the wrong cause and the cause of your concrete problem has to be sought in a different direction.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Would it make sense to implement NamingContainer in my component? – user1323246 Jul 02 '12 at 14:08
  • You're free to create a custom one yourself, the benefit is designtechnically however highly questionable. You could alternatively also just wrap it in a ``. – BalusC Jul 02 '12 at 14:09
  • I am not using composiste-components. Would it be possible to do this programmatically as well? – user1323246 Jul 02 '12 at 14:12
  • No, you really need to create a custom component. Note that this is not the same as a composite component. – BalusC Jul 02 '12 at 14:15
  • whats the difference between extending a standard component (htmlpanelgroup) and creating a custom component? – user1323246 Jul 02 '12 at 14:24
  • Nothing. It's basically the same. You're free to extend existing components or not. To minimize boilerplate code, most obvious choice would be to extend existing components for your custom component. But once again, I have the strong feeling that you're looking for a solution in the completely wrong direction. – BalusC Jul 02 '12 at 14:24
  • I've added some information, hope that what I am trying to accomplish is more clear now – user1323246 Jul 03 '12 at 06:32
  • Oh, you've thus already created a custom component before and were having problems with it. Why didn't you tell anything about that in your initial question? Your initial question didn't give *any* clues that you were already using a custom component. This way just plain JSF will always be assumed. Well, apart from a custom component, you could also create a tag file. This is after all probably easier. You can get some ideas out of this examlpe http://stackoverflow.com/questions/5713718/how-to-make-a-grid-of-jsf-composite-component/5716633#5716633 – BalusC Jul 03 '12 at 12:36
0

Specify explicitly id in <h:form> and set prependId=false in h:form

jmj
  • 237,923
  • 42
  • 401
  • 438
0

As you mentioned in you comment on BalusC answer, it would be possible to have the custom component implement NamingContainer. I tried something similar and since your custom component is basically nothing more that a composite-component your surrounding HtmlPanelGroup will be the NamingContainer for the internal composed components.

Be carefull though and do not override getFamily with another name. This drove me nuts until I figured out that when you do that, JSF cannot find the standard renderer (the one registered with HtmlPanelGroup).

public class MyComponenten extends HtmlPanelGroup implements NamingContainer {

    public void encodeBeginn(FacesContext context){
        ...fill with components
        super.encodeBeginn(context);
    }
...
lostiniceland
  • 3,729
  • 6
  • 38
  • 50