DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > How to Programmatically Access PrimeFaces Tags

How to Programmatically Access PrimeFaces Tags

A. Programmer user avatar by
A. Programmer
·
Apr. 27, 12 · Java Zone · Tutorial
Like (0)
Save
Tweet
21.15K Views

Join the DZone community and get the full member experience.

Join For Free

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 
        </f:facet> 
        <c:forEach items="#{tableViewBean.simpleColumns}" var="column">                                                                                     
          <p:column headerText="#{column.header}">                                            
            <h:outputText value="#{t[column.property]}"/> 
          </p:column>                                              
        </c:forEach>
        <f:facet name="footer"> 
          <c:when test="#{not empty tableViewBean.selectedTable}">
            <h:outputText value="#{tableViewBean.selectedTable}" />
          </c:when>
        </f:facet>
      </p:dataTable>
     </h:form>
    </h:panelGrid>
   </p:tab>
  </p:accordionPanel>                            
 </p:tab>
...


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

((DataTable)(FacesContext.getCurrentInstance().getViewRoot().findComponent(
"centerTabIds:dataId:tableViewFormId:tableViewId"))).setFirst(0);


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"/>
</c:forEach>
...


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!

PrimeFaces

Published at DZone with permission of A. Programmer. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Evolving Domain-Specific Languages
  • 6 Things Startups Can Do to Avoid Tech Debt
  • Stupid Things Orgs Do That Kill Productivity w/ Netflix, FloSports & Refactoring.club
  • What Is ERP Testing? - A Brief Guide

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo