Managed property value not updated when both beans are of same scope

随声附和 提交于 2019-12-04 17:45:04

@ManagedProperty annotation can only provide static injection, which means that the annotated property will get injected when and only when the holding @ManagedBean is instantiated.

When you deploy your application, I believe your CartBean was referenced right at the beginning through things like the View cart button, etc. As a consequence, the injection took place too early and since the bean is @SessionScoped, you will carry the initial false value till the end of time :).

Instead of injecting only the boolean field, you should, instead, inject the whole UserManager bean:

@ManagedBean
@SessionScoped
public class CartBean extends BaseBean implements Serializable {
   @ManagedProperty(value = "#{userManager}")
   private UserManager userManager;

   public void updateCart(Movie selectedMovie) {
       if (!userManager.isLoggedIn()) {
           return;
       }

       ...
   }
}

The solution is using Omnifaces it worked for me each time the value change you will get the new value

@ManagedBean
@ViewScoped
public class CartBean extends BaseBean implements Serializable {

   private boolean loggedIn;

   public void updateCart(Movie selectedMovie) {
loggedIn=Faces.evaluateExpressionGet("#{userManager.loggedIN}");
       if (!userManager.isLoggedIn()) {
           return;
       }

       ...
   }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!