{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner
java,frameworks,tips and tricks

How to Programmatically Access PrimeFaces Tags

This is FYI kind of tip. Recently, I tried to programmatically acess PrimeFaces tags from Java beans. I found two solutions that can be extrapolated to almost all PrimeFaces tags.

1. Use the findComponent() method

Per example, the below code contains a DataTable, and I want to programmatically to set the zero-relative row number of the first row to be displayed.

<p:layoutUnit position="center" styleClass="welcome">
 <p:tabView id="centerTabIds" activeIndex="2">
  <p:tab title="LBD">
   <p:accordionPanel id="dataId">
    <p:tab title="Data Table Viewer">
     <h:panelGrid columns="1" cellpadding="5" 
                                style="margin-left: auto; margin-right: auto;">
      <h:form id="tableViewFormId" prependId="false">
       <p:dataTable id="tableViewId" var="t" value="#{tableViewBean.lazyModel}" 
                paginator="true" rows="5" 
                currentPageReportTemplate="{currentPage} of {totalPages}" 
                paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} 
                {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                rowsPerPageTemplate="5,10" emptyMessage="No records found" 
                paginatorPosition="top" first="0"> 
        <f:facet name="header">  
          Data Table 
        <c:forEach items="#{tableViewBean.simpleColumns}" var="column">                                                                                     
          <p:column headerText="#{column.header}">                                            
            <h:outputText value="#{t[column.property]}"/> 
        <f:facet name="footer"> 
          <c:when test="#{not empty tableViewBean.selectedTable}">
            <h:outputText value="#{tableViewBean.selectedTable}" />

And here it is the programmatically solution using the findComponent() method:


This is a tip in a tip – using multiple dynamic data tables and switching between them will get you to an unpleasant situation. PrimeFaces does not reset the data table to the first page every time you switch to another table, but this code fixes that problem.

Even if it is not a good idea to hardcode components ids, this can save a lot of time when no solution is available.

2. Use ActionEvent

Another common scenario is like this: I have a PrimeFaces menu, with several menu items. Each menu item has a value attribute, and all of them indicates the same action listener. I want to get the value of the menu item that was selected. The code looks like this:

<c:forEach items="#{iccAnalysisBean.wellIds}" var="row">                                                                                     
  <p:menuitem value="#{row.toString()}" 
            actionListener="#{iccAnalysisBean.selectedWellAction}" ajax="true"/>

And I’ve accomplished my task like this:

public void selectedWellAction(ActionEvent e) {
  String valueOfWell = String.valueOf(e.getComponent().getAttributes().get("value"));

I hope that this will help you when you are stack in such issues!

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks