Time for action – using the stateless ThemeSwitcher component
Let us see how we can change theme dynamically using the ThemeSwitcher component.
- Create a
<p:themeSwitcher>
component by providing list of theme names as options:<p:themeSwitcher style="width:165px"> <f:selectItem itemLabel="Choose Theme" itemValue="" /> <f:selectItems value="#{userPreferences.themes}" /> </p:themeSwitcher>
- Implement a managed bean method to return the list of all Primefaces supporting theme names:
@ManagedBean @SessionScoped public class UserPreferences implements Serializable { private List<String> themes; public UserPreferences() { themes = new ArrayList<String>(); themes.add("afterdark"); themes.add("afternoon"); themes.add("afterwork"); themes.add("aristo"); themes.add("black-tie"); themes.add("blitzer"); themes.add("bluesky"...