0

How can I dynamically set a Primefaces theme? Ultimately, I'd like to be able for my application to toggle from dark mode to light mode, depending on the users' OS setting.

ThemeBean.java:

import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Named;
import java.io.Serializable;
@Named
@SessionScoped
public class ThemeBean implements Serializable {
 private String theme;
 public String getTheme() {
 return theme;
 }
 public void setTheme(String theme) {
 this.theme = theme;
 }
}

web.xml:

 <context-param>
 <param-name>primefaces.THEME</param-name>
 <param-value>#{themeBean.theme}</param-value>
 </context-param>

pom.xml:

 <dependency>
 <groupId>org.primefaces</groupId>
 <artifactId>primefaces-themes</artifactId>
 <version>15.0.10</version>
 </dependency>

However, I keep getting this message:

Error Unable to find resource primefaces-, theme.css

I understand web.xml doesn't support EL expressions mentioned here; however, PrimeFaces showcases uses this approach:

Automatically sets a light or dark theme based on the OS settings. Put it in your template like:

<pe:lightSwitch selected="\#{userBean.theme}"/>

userBean should be a session scoped bean with a String type theme property (with both getter and setter). Then set the theme in your web.xml like:

<context-param>
 <param-name>primefaces.THEME</param-name>
 <param-value>\#{userBean.theme}</param-value>
</context-param>
asked Dec 2, 2025 at 21:40
3
  • 1
    Couple of questions why the "\" in front of the "#"? Second why using @ManagedBeans and not CDI beans? Commented Dec 3, 2025 at 14:11
  • 1. I guess it was a typo from the showcase. 2. Good point However, even then, I get the same message: Error Unable to find resource primefaces-, theme.css Commented Dec 3, 2025 at 14:30
  • Maybe download and run the showcase? It definitely theme switches? Commented Dec 4, 2025 at 16:29

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.