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

Tuesday, December 18, 2018

Dependencies for Spring Boot Project

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

</dependencies>
  • spring-boot-starter         – this lets spring boot autoconfigure your application
  • spring-boot-starter-web – this dependency tells spring boot that your application is a web application. This add the functionality of @Controller, @RequestMapping, etc.
  • spring-boot-starter-thymeleaf – web app needs some views like jsp or html. Thymeleaf is a server side template engine where your htmls can be viewed in any browser. Simply to say, instead of traditional jsp with jstl or format tags.
  • spring-boot-devtools     – this is an optional dependency but this makes your development faster. This dependency automatically restart or hotswap your changes to the running tomcat server, thus removing the need to restart the application whenever there are changes in codes.

Thursday, December 6, 2018

Session Handling in Spring Boot Application

There  are several ways to  handle session in  spring boot application.

One way  that we can  use  is  handling session  through Spring Security .


1.Handling Session  Through using  Redis



Spring Session has the simple goal of free up session management from the limitations of the HTTP session stored in the server.

The solution makes it easy to share session data between services in the cloud without being tied to a single container (i.e. Tomcat).
 Additionally, it supports multiple sessions in the same browser and sending sessions in a header.


2.Handling Session  Through Spring Security


When Is The Session Created?

We can control exactly when our session gets created and how Spring Security will interact with it:

    always           – a session will always be created if one doesn’t already exist
    ifRequired     – a session will be created only if required (default)
    never             – the framework will never create a session itself but it will use one if it already exists
    stateless        – no session will be created or used by Spring Security

<http create-session="ifRequired">...</http>

Java configuration:

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)

}

It’s very important to understand that this configuration only controls what Spring Security does –
not the entire application. Spring Security may not create the session if we instruct it not to, but our app may!
By default, Spring Security will create a session when it needs one – this is “ifRequired“.For a more stateless
application, the “never” option will ensure that Spring Security itself will not create any session; however, if
the application creates one, then Spring Security will make use of it.

Finally, the strictest session creation option – “stateless” – is a guarantee that the application will not create
any session at all.

This was introduced in Spring 3.1 and will effectively skip parts of the Spring Security filter chain – mainly the
session related parts such as

 HttpSessionSecurityContextRepository, SessionManagementFilter, RequestCacheFilter.

These more strict control mechanisms have the direct implication that cookies are not used and so each and every
request needs to be re-authenticated.

This stateless architecture plays well with REST APIs and their Statelessness constraint. They also work well with
authentication mechanisms such as Basic and Digest Authentication


References Used :-   spring-security-session
                                  spring-session

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...