Book Image

PrimeFaces Cookbook

Book Image

PrimeFaces Cookbook

Overview of this book

PrimeFaces is the de facto standard in the Java web development. PrimeFaces is a lightweight library with one jar, zero-configuration, and no required dependencies. You just need to download PrimeFaces, add the primefaces-{version}.jar to your classpath and import the namespace to get started. This cookbook provides a head start by covering all the knowledge needed for working with PrimeFaces components in the real world. "PrimeFaces Cookbook" covers over 100 effective recipes for PrimeFaces 3.x which is a leading component suite to boost JSF applications. The book's range is wide‚Äí from AJAX basics, theming, and input components to advanced usage of datatable, menus, drag & drop, and charts. It also includes creating custom components and PrimeFaces Extensions.You will start with the basic concepts such as installing PrimeFaces, configuring it, and writing a first simple page. You will learn PrimeFaces' theming concept and common inputs and selects components. After that more advanced components and use cases will be discussed. The topics covered are grouping content with panels, data iteration components, endless menu variations, working with files and images, using drag & drop, creating charts, and maps. The last chapters describe solutions for frequent, advanced scenarios and give answers on how to write custom components based on PrimeFaces and also show the community-driven open source project PrimeFaces Extension in action.
Table of Contents (17 chapters)
PrimeFaces Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Remembering current menu selection


PrimeFaces' Menu component is often used for navigation. The current menu selection helps to identify the current navigation target, which is most likely to be a web page. Menu in PrimeFaces does not remember the selected menu item in terms of visual selection. One of the requirements for the Menu component, therefore, is to display the currently clicked menu item as selected.

In this recipe, we will implement a solution for remembering the current menu selection. This will improve the menu's usability during navigation between pages.

How to do it...

Assume we have a menu with three menu items. Each menu item has URL-based navigation to a specific page. To mark a menu item as selected, we need a client-side callback bound to onclick and a styleClass with server-side binding.

<p:menu id="viewList">
  <p:menuitem value="View 1" 
  styleClass="#{naviController.getMenuitemStyleClass('page1')}"
    onclick="selectMenuitemLink(this)" ajax="false" 
    url...