I followed the same example in the website that uses the GuestPreferences to set the theme on the session I guess.
But it doesn't worked well.
It changes the theme but, when I press F5 the theme turns to the original.
See my code:
ThemeSwitcherMB - Manange Bean
Code: Select all
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mb;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;
import util.GuestPreferences;
/**
*
* @author Fabricio
*/
@Named(value = "themeSwitcherMB")
@SessionScoped
public class ThemeSwitcherMB implements Serializable {
private Map<String, String> themes;
private String theme;
private GuestPreferences gp = new GuestPreferences();
public void setGp(GuestPreferences gp) {
this.gp = gp;
}
public Map<String, String> getThemes() {
return themes;
}
public String getTheme() {
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
/** Creates a new instance of ThemeSwitcherMB */
public ThemeSwitcherMB() {
theme = gp.getTheme();
gp.setTheme("blitzer");
themes = new TreeMap<String, String>();
themes.put("Aristo", "aristo");
themes.put("Blitzer", "blitzer");
themes.put("Smoothness", "smoothness");
themes.put("Start", "start");
themes.put("UI-Lightness", "ui-lightness");
themes.put("Vader", "vader");
}
public void saveTheme() {
System.out.println("theme: " + theme);
gp.setTheme(theme);
}
}
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition template="#{pageContext.request.contextPath}/template/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<ui:define name="content">
<p:panel id="conteudo">
<p:messages id="msgConfig" globalOnly="true"/>
<h:form id="formDados">
<p:panel style="border: none; padding: 0px; margin: 0px;">
<p:growl id="msgs"/>
</p:panel>
<p:panel header="Configuração - Personal"
style="padding: 0px; margin: 0px;">
<p:panel>
<h:panelGrid columns="2">
<h:outputLabel value="Escolha um tema: "/>
<p:themeSwitcher value="#{themeSwitcherMB.theme}" style="width:150px" effect="fade">
<f:selectItem itemLabel="Choose Theme" itemValue="" />
<f:selectItems value="#{themeSwitcherMB.themes}" />
<p:ajax listener="#{themeSwitcherMB.saveTheme}" />
</p:themeSwitcher>
</h:panelGrid>
</p:panel>
</p:panel>
<br/>
<p:commandButton image="ui-icon ui-icon-disk"
value="Gravar"
action="#{configValidacaoMB.cadastraConfig}"
update="formDados"
process="formDados"
oncomplete="
if(args.sucesso == true) {
self.location= '../home/index.xhtml';
}
"
onstart="showLoading('formDados:imagemP2');"
onsuccess="hideLoading('formDados:imagemP2');"/>
<p:graphicImage id="imagemP2" value="/imagens/ajaxloading.gif" style="margin-left: 10px; display: none;"/>
<p:commandButton image="ui-icon ui-icon-cancel"
value="Cancelar"
process="@this"
oncomplete="self.location= '../home/index.xhtml';"
onstart="showLoading('formDados:imagemP1');"
onsuccess="hideLoading('formDados:imagemP1');"/>
<p:graphicImage id="imagemP1" value="/imagens/ajaxloading.gif" style="margin-left: 10px; display: none;"/>
</h:form>
</p:panel>
</ui:define>
</ui:composition>
Code: Select all
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package util;
/**
*
* @author Fabricio
*/
import java.io.Serializable;
import java.util.Map;
import javax.faces.context.FacesContext;
public class GuestPreferences implements Serializable {
private String theme = "vader"; //default
public String getTheme() {
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
if(params.containsKey("theme")) {
theme = params.get("theme");
}
return theme;
}
public void setTheme(String theme) {
this.theme = theme;
}
}
Code: Select all
...
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>smoothness</param-value>
</context-param>
..