Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

How to Programmatically Access PrimeFaces Tags

  • submit to reddit
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[]}"/> 
        <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 its author, Constantin Alin. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Christian Beute... replied on Fri, 2012/04/27 - 4:16am

Should not the binding Attribute allow for this?

I know it's hard to get right with getters and setters.

Another possibility I have used is an external factory called by  a custom component.





Jonathan Fisher replied on Fri, 2012/04/27 - 6:48pm in response to: Christian Beutenmueller

Another tip, avoid using c: (like c:foreach) namespace in JSF! The JSTL tags are not safe with @ViewScoped beans for a variety of problems. Instead use equivelants ui: namespace.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.