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}"/>
userBeanshould be a session scoped bean with aStringtype theme property (with both getter and setter). Then set the theme in yourweb.xmllike:<context-param> <param-name>primefaces.THEME</param-name> <param-value>\#{userBean.theme}</param-value> </context-param>
"\"in front of the "#"? Second why using @ManagedBeans and not CDI beans?