I have two templates in my web project. I want to use a different PrimeFaces theme for each template.
How do I do this?
No web.xml
, when I put the theme of PrimeFaces it changes to all templates, but I want each template to have a theme.
I have two templates in my web project. I want to use a different PrimeFaces theme for each template.
How do I do this?
No web.xml
, when I put the theme of PrimeFaces it changes to all templates, but I want each template to have a theme.
You can do all this processing in ManagedBean
. For example:
UserMB:
@ManagedBean
@SessionScoped
public class UsuarioMB {
private String templateSelecionado = "templateDefault.css";
private String temaSelecionado = "temaDoTemplateDefault";
private List<String> temasDisponiveis = new ArrayList<String>();
@PostConstruct
public void init() {
if(condicao) { //condição para trocar de tema/template
templateSelecionado = "templateDiferente.css";
temaSelecionado = "temaDoTemplateDiferente";
}
//Populando os temas
temasDisponiveis.add("casablanca");
temasDisponiveis.add("cupertino");
temasDisponiveis.add("bluesky");
}
//getters & setters
}
template.xhtml
<h:outputStylesheet library="css" name="#{usuarioMB.templateSelecionado}" />
pageToSelectTema.xhtml
<p:themeSwitcher value="#{usuarioMB.temaSelecionado}">
<f:selectItems value="#{usuarioMB.temasDisponiveis}" />
</p:themeSwitcher>
web.xml
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>#{usuarioMB.temaSelecionado}</param-value>
</context-param>
Explanation :
Using the templateSelecionado
and temaSelecionado
attributes, both of type String
, you can dynamically modify both the css and the theme of PrimeFaces.
Simply make the <h:outputStylesheet>
tag use the templateSelecionado
attribute and make the page that changes the theme (containing the <p:themeSwitcher>
component) and web.xml
use the temaSelecionado
attribute. / p>