O'Reilly logo

PrimeFaces Beginner's Guide by K. Siva Prasad Reddy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – applying a user-specific theme using stateful ThemeSwitcher

In this section we will learn how to use the ThemeSwitcher component to apply theme for entire application based on user preference, and how a user can dynamically switch themes:

  1. Configure Primefaces theme name in web.xml using EL expression:
    <context-param>
      <param-name>primefaces.THEME</param-name>
      <param-value>#{userPreferences.selectedTheme}</param-value>
    </context-param>

    Here EL expression #{userPreferences.selectedTheme} resolves to a theme name.

  2. Create ThemeSwitcher component using <p:themeSwitcher> with value="#{userPreferences.selectedTheme}" along with the AJAX event listener to update the user-preferred theme to the selected theme name on server side:
    <p:themeSwitcher ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required