【问题标题】:Primefaces Theme change to user specificPrimefaces 主题更改为用户特定
【发布时间】:2020-12-11 06:34:10
【问题描述】:

当我使用下面的代码时,它可以正常工作并更改主题,但问题是如果其中一个用户登录并更改主题,它会影响系统中的每个用户。我想要的是仅针对特定用户而不是针对每个用户来影响主题。

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 类

@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);
       }
   }

}

【问题讨论】:

  • 这绝对是奇怪的,因为你有它 SessionScoped 所以每个用户都应该有自己独特的 SessionController 和个人价值。我在我的应用程序中执行此操作,但没有看到此问题。
  • 谢谢您的回复。是的,看起来还可以。但是在我的应用程序中,一个用户更改了主题,这对每个用户都有影响。关于它的任何想法..
  • 我唯一能想到的是由于某种原因 web.xml 没有得到每个用户的尊重。我不使用 Spring,但这绝对适用于 JoinFaces,并且它适用于我编写的 J2EE PF 应用程序。所以也许这并没有真正尊重每个用户。

标签: spring-boot primefaces themes


【解决方案1】:

默认情况下,每个 Spring bean 都是一个Singletone,这就是为什么所有用户都会受到影响,尽管@SessionScoped

你不能同时使用@ManagedBean@Controllersee why

在同一个应用中结合 SpringJSF 的最佳方式是使用Joinfaces

使用 Joinfaces,您的 bean 最终会看起来像这样

@Named
@SessionScoped
public class SettingsController {

相关:

JSF vs. Spring MVC

【讨论】:

    猜你喜欢
    • 2016-06-17
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2012-03-10
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多