0

When I use the below code it's working fine and change the theme, but the problem is if one of the user logged and change the theme it effects to the every user in the system. what I want is to effect the theme only for the particular user but not for every one.

Web.xml

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

primeface (.xhtml)

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

SettingsController.java class

@ManagedBean(name = "settingsController")
@SessionScoped
@Controller
public class SettingsController {

   private String userTheme = "start" ;  
   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 (){
       ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
       try {
           ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
       } catch (IOException ex) {
           Logger.getLogger(SettingsController.class.getName()).log(Level.SEVERE, null, ex);
       }
   }

}

Trilok
  • 53
  • 1
  • 6
  • That is definitely strange as your have it SessionScoped so each user should get their own unique SessionController and thus individual value. I do this in my apps and I have not seen this issue. – Melloware Dec 11 '20 at 12:08
  • Thank you, for the response. Yes, It's seems ok. but in my application one user change the theme, it's effect to every users. any idea about it.. – Trilok Dec 11 '20 at 13:33
  • Only thing I can possibly think of is for some reason web.xml is not being respected per user. I don't use Spring but this definitely works in JoinFaces and it straigght J2EE PF apps that I have authored. So maybe that is not truly respecting it per user. – Melloware Dec 11 '20 at 18:13

1 Answers1

1

Every Spring bean is a Singletone by default, that's why all users are affected, despite of @SessionScoped.

You can't use @ManagedBean and @Controller at the same time, see why.

The best way to combine Spring and JSF in the same app, is to use Joinfaces.

With Joinfaces your bean will end up looking like this

@Named
@SessionScoped
public class SettingsController {

Related:

JSF vs. Spring MVC

stefan-dan
  • 586
  • 5
  • 19