Book Image

Primefaces Cookbook Second Edition

Book Image

Primefaces Cookbook Second Edition

Overview of this book

Table of Contents (20 chapters)
PrimeFaces Cookbook Second Edition
Credits
Foreword
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating programmatic menus


Programmatic menus offer a more flexible way in comparison with the declarative approach. The whole menu structure can be created in Java and bound as a model to the p:menu tag. Programmatic menu creation is the best choice when we load a menu definition from a database or XML file, and if the menu structure is not known beforehand.

In this recipe, we will learn about the PrimeFaces menu model and create a programmatic menu.

How to do it…

Every programmatically created menu instance should implement the org.primefaces.model.MenuModel Java interface. PrimeFaces provides a default implementation, org.primefaces.model.DefaultMenuModel, that is sufficient to use in most cases. Your own customized implementations of MenuModel are possible as well. Let's create a static menu from the Statically and dynamically positioned menus recipe in a programmatic way:

@Named
@ViewScoped
public class ProgrammaticMenuBean implements Serializable {

  private MenuModel model;

  @PostConstruct...