当两个bean的作用域相同时,托管属性值不会更新 [英] Managed property value not updated when both beans are of same scope

查看:104
本文介绍了当两个bean的作用域相同时,托管属性值不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用@ManagedProperty时,我看到这种奇怪的行为.我有2个豆子:

I am seeing this strange behavior when using @ManagedProperty. I have 2 beans:

UserManager (SessionScoped)

UserManager (SessionScoped)

@ManagedBean
@SessionScoped
public class UserManager extends BaseBean implements Serializable
{
   private static final long serialVersionUID = 1861000957282002416L;

   private User currentUser;

   public String login() 
   {
      // set value of currentUser after authentication
   }

   public User getCurrentUser() {
      return currentUser;
   }

   public boolean isLoggedIn() {
      return getCurrentUser() != null;
   }
}

CartBean ( ALSO SessionScoped)

CartBean (ALSO SessionScoped)

...
import javax.faces.bean.ManagedProperty;
...

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

   public void updateCart(Movie selectedMovie)
   {
      if (!loggedIn) {
         return;
      }

      System.out.println("UPDATE CART REQUEST");

      int id = selectedMovie.getMovieID();

      if (cart.containsKey(id)) {
         cart.remove(id);
      }
      else {
         cart.put(id, selectedMovie);
      }
   }

   public void setLoggedIn(boolean loggedIn) {
      this.loggedIn = loggedIn;
   }
}

成功登录后,loggedIn still 的值仍为false.

After logging in successfully, the value of loggedIn still remains false.

但是,如果我将CartBean的范围更改为@ViewScoped,则会更新loggedIn的值,并且会看到sysout.

However, if I change the scope of CartBean to @ViewScoped, the value of loggedIn gets updated and I see the sysout.

根据我的理解,并且在阅读了各种文章之后,只有在相同更广泛范围内,才可以注入托管bean或其属性.但是相同范围"的情况在我的代码中似乎不起作用.我在这里想念什么?

As per my understanding and also after reading various articles, one can inject a managed bean or its property only if it is of the same or broader scope. But the "same scope" case does not seem to work in my code. What am I missing here?

我正在使用:

  • Mojarra 2.1.16
  • 3.2春季
  • 休眠4.1
  • Tomcat 7.0.37

推荐答案

@ManagedProperty注释只能提供静态注入,这意味着带注释的属性将在当且仅当保存时被注入@ManagedBean被实例化.

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

在部署应用程序时,我相信一开始就通过View cart按钮之类的东西引用了CartBean.因此,注入太早了,因为bean是@SessionScoped ,您将一直保留false的初始值,直到时间结束:).

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

您应该注入整个UserManager bean,而不是只注入boolean字段:

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

       ...
   }
}

这篇关于当两个bean的作用域相同时,托管属性值不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆