Thursday, December 27, 2018

How to Change PrimeFace Theme According to User Preferences in SpringBoot Project


In web.xml  file  add  this   ,

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>#{settingsController.userTheme}</param-value>
    </context-param>


Our  controller  class  will be like this ,

@ManagedBean(name = "settingsController")
@SessionScoped
@Controller
public class SettingsController {
    
    private String userTheme = "glass-x" ;  //Initial Theme 
    private Map<String , String>  themeMap ;
    
    @PostConstruct
    public void init (){
       setThemeMapInit(  );
    }
    
    public String getUserTheme() {
        return userTheme;
    }

    public void setUserTheme(String userTheme) {

        this.userTheme = userTheme;
    }

    public Map<String, String> getThemeMap() {

        return themeMap;
    }
  
    public void setThemeMapInit() {
        themeMap =  new LinkedHashMap<String, String>();
        themeMap.put("Aristo", "aristo");
        themeMap.put("After-noon", "afternoon");
        themeMap.put("After-Work", "afterwork");
        themeMap.put("Black-Tie", "black-tie");
        themeMap.put("Blitzer", "blitzer");
        themeMap.put("Bluesky", "bluesky");
        themeMap.put("Bootstrap", "bootstrap");
        themeMap.put("Casablanca", "casablanca");
        themeMap.put("Cupertino", "cupertino");
        themeMap.put("Dark-Hive", "dark-hive");
        themeMap.put("Delta", "delta");
        themeMap.put("Excite-Bike", "excite-bike");
        themeMap.put("Flick", "flick");
        themeMap.put("Glass-X", "glass-x");
        themeMap.put("Home", "home");
        themeMap.put("Hot-Sneaks", "hot-sneaks");
        themeMap.put("Humanity", "humanity");
        themeMap.put("Overcast", "overcast");
        themeMap.put("Pepper-Grinder", "pepper-grinder");
        themeMap.put("Redmond", "redmond");
        themeMap.put("Rocket", "rocket");
        themeMap.put("Sam", "sam");
        themeMap.put("Smoothness", "smoothness");
        themeMap.put("South-Street", "south-street");
        themeMap.put("Start", "start");
        themeMap.put("Sunny", "sunny");
        themeMap.put("Swanky-Purse", "swanky-purse");
        themeMap.put("UI-Lightness", "ui-lightness");

    }

    
    public void setThemeMap(Map<String, String> themeMap) {  
     this.themeMap =themeMap;
    }
    
    public void  sumbitUserSettings (){
        System.out.println("******  User  Theme ****** " +  userTheme );
    }

}

 


Finally  our xhtml  file  will  be like  this

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <body>

        <ui:composition template="./mainTemplate.xhtml">

            <ui:define name="content">


                <h1 class="ui-widget ui-widget-header" style=" font-size: 15px;

                    font: bold;line-height: 2.8em;text-align: center;">Settings</h1>


                <h:form id="changeThemeFormId" >


                    <p:growl id="
changeThemeFormMsg" showDetail="true"   showSummary="false" />
                   
                        <h:panelGrid columns="3" cellpadding="5" styleClass="add-form">   
                            
                            <h:outputLabel for="userTheme" value="Theme Name  *:" style="width: 300px"/> 
                            <p:selectOneMenu id="userTheme" value="#{settingsController.userTheme}"  style="width:200px" 
                                             required="true" requiredMessage="Theme Name is Required" > 
                                <f:selectItems value="#{settingsController.themeMap}"/> 
                            </p:selectOneMenu>
                            <p:message for="userTheme" display="icon"/>

                            <p:commandButton value="Sumbmit" styleClass="ui-priority-primary" style="font-weight:bold; font-size:13px" 

                                            resetValues="ttrue"  actionListener="#{settingsController.sumbitUserSettings()}"/>

                        </h:panelGrid> 


                    </p:fieldset>


                </h:form>


            </ui:define>


        </ui:composition>

    </body>
</html>


Version  1.1.0

No comments:

Post a Comment

The AI Driven Software Developer, Optimize Innovate Transform

  The AI-Driven Software Developer: Optimize, Innovate, Transform": AI Transformation in Software Development : Understand how AI is re...