Acquire full prefix for a component clientId inside naming containers with JSF 2.0

前端 未结 2 439
时光说笑
时光说笑 2020-12-09 18:08

I am updating a component via AJAX in JSF:


    Click me
        
             


        
相关标签:
2条回答
  • 2020-12-09 18:19

    @Tuukka Mustonen, thank you for your answer.

    And if we need access some another id from the same "place" we could use:

    <h:outputScript name="js/my-script.js"/>
    <h:form id="myForm">
        <h:inputText id="aaa"
            onkeyup="myJSFunction(this.id)"
            value="..."
        />
        <h:inputText id="bbb"
            onkeyup="myJSFunction(aaa.id)"
            value="..."
        />
        <h:inputText id="ccc"
            onkeyup="myJSFunction('#component.parent.clientId.concat(':ccc')}')"
            value="..."
        />
        <h:inputText id="ffffd"
            onkeyup="myJSFunction('#component.parent.clientId.concat(':aaa')}')"
            value="..."
        />
    </h:form>
    

    JavaScript function:

    function myJSFunction(message) {
        window.alert(message)
    }
    

    Output on dialog window:

    1) myForm:aaa

    2) null

    3) myForm:ccc

    4) myForm:aaa

    Note: so the 1st and 3th have the same output.

    0 讨论(0)
  • 2020-12-09 18:33

    Seems like you can access the current prefix via Expression Language (EL) implicit objects (cc and component):

    • cc.clientId returns current composite component's prefix
    • component.clientId returns prefix for any current component.

    For example, in a page, call some component via

    <myComponent id="foo">
    

    Inside this component, one can fetch the client IDs like this:

    <h:outputText id="bar">
       <p>ClientId for the composite component: #{cc.clientId}</p>
       <p>ClientId for current any component: #{component.clientId}</p>
    </h:outputText>
    

    The following should print out as:

    ClientId for the composite component: foo
    ClientId for current any component: foo:bar
    

    I got the pointer from blog post JSF: working with component identifiers (id/clientId). It states that this is a new feature for JSF 2.0. Before that one had to fetch the ID programmatically from a backing bean.

    0 讨论(0)
提交回复
热议问题