Search code examples
jsfprimefacesjsf-2

<f:attribute> not working inside <p:tab>


I want to access some parameters in my backing bean on tab click.I used but i'm not able to fetch parameters from it.Here's my code:

<p:tabView>
<p:ajax event="tabChange" listener="#{maintab.tabchangelistener}" />
<c:forEach items="#{maintab.random}" var="field">
    <p:tab title="Title">
        <p:tabView  prependId="false" cache="false" dynamic="true">
            <p:tab title="ABC" style="outline: 0;">
                <f:attribute name="parameter" value="#{field.name}"/>
            </p:tab>
        </p:tabView>
    </p:tab>
</c:forEach> 
</p:tabView>

My tabchangelistener function is :

public void tabchangelistener(TabChangeEvent event){
    String par=(String)event.getTab().getAttributes().get("parameter");
}

Solution

  • I would suggest some javascript approach here using JSF pass-through attributes with the namespace xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"

    Something as below:

    <p:tabView 
     onTabChange="handleTabChange(index)"
     onTabShow="handleTabShow(index)">
      <c:forEach items="#{maintab.random}" var="field">
         <p:tab title="Title">
            <p:tabView  prependId="false" cache="false" dynamic="true">
                <p:tab title="ABC" style="outline: 0;" pt:data-name="#{field.name}" />
            </p:tabView>
         </p:tab>
      </c:forEach> 
    </p:tabView>
    

    Then, on the javascript functions handleTabChange and handleTabShow, you can just use some plain jQuery code to find the tab you want, and get the corresponding data-name pass-through attribute.

    Once you have it, you can trigger some <p:remoteCommand> calling whatever action listener you want, passing the field as a parameter, so you can get it inside the action listener as a request parameter.

    Something like:

    <p:remoteCommand name="remoteTabActionListener" actionListener="#{maintab.tabActionlistener}" />
    
    function handleTabShow(index) {
        var pass_through_field = $("expression to find the correct tab").attr("data-name");
        remoteTabActionListener([{name: 'my_field', value: pass_through_field}]);
    }
    

    So finally, in your java action listener, you can retrieve the field:

    FacesContext fc = FacesContext.getCurrentInstance();
    Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
    String page = params.get("my_field");
    

    Not sure if this will work, but maybe can give you some ideas on how to achieve your goal ...