JSF 2.0 webapplication value is null after changed to the next managed bean

好久不见. 提交于 2019-12-25 01:55:36

问题


I have made a web application. In my first version I used one @ManagedBean with @SessionScoped annotation. But after I put some features to the web application I had too much methods in my managed bean. So I shared the Bean in six Beans with a @SessionScope. The problem is, if I used the first managed bean call LoginBean. I have no problems. But after I changed on the next page called SampleForDB. For this page I used the SampleBean. In this manged bean (SampleBean) the values of the classes I set in the loginBean are null.

How can I made it to use the values in all @MangedBean classes?

@ManagedBean
@SessionScoped
public class Example{

   private TestClass test = new TestClass();

   public next(){
      test.setmyVar(5);
   }
}


@ManagedBean
@SessionScoped
public class Example2{
   int testInt;
   private TestClass test = new TestClass();

   public after(){
      int testInt = test.getmyVar(); // I get null
   }
}


public class TestClass{
   private int myVar;

   public void setMyVar(int var){
      this.myVar = var;
   }

   public int getMyVar(){
      return myVar;
   }
}

This is an example for my Problem. My variable myVar is empty when I want to get it work in another Managed Bean.


回答1:


There's no SampleBean or LoginBean in your code sample, but your problem is caused by the new operator in any case.

Using new to get @ManagedBean instances tells JSF "hey, I don't need to manage those objects". Each time JSF constructs a new java object from Example2 it'll also create a new TestClass instance. To access your existing bean, use something like this:

@ManagedBean @SessionScoped
public class LoginBean implements Serializable {
    private String userLogin;

    public void doLogin() { /* assign userLogin */ }
}

@ManagedBean @SessionScoped
public class SampleBean implements Serializable {
    @ManagedProperty("#{loginBean.userLogin}")
    private String login;
    private String greeting;

    public void sayHello() {
        greeting = "Hello " + login;
    }
}

You typically don't want a full bean injected somewhere, concentrate on the attributes you really need to get the job done.

Recommended reading:

JavaEE tutorial

How to choose the right bean scope?




回答2:


You probably don't need to use so many managed beans. Just use one managed bean and include other classes in it. One managed bean per JSF page.

As you provided the code for you question: the problem is that you have two different instances of your TestClass. So when you set value in first instance it doesn't set in the second. That's why you get null there. What you should do is to share the same object instance between different managed beans. It can be done different ways:

  • Put your TestClass in session context in first managed bean and retrieve it from context in the second managed bean.
  • Apply singleton pattern and get the same instance with .getInstance() method.
  • When you created the instance of TestClass in the first managed bean, set the same value for the second managed bean from the first one.
  • Use dependency injection mechanism in your application (CDI or Spring).
  • Mark your TestClass as @ManagedBean with @SessionScoped or @ApplicationScoped and then use in other of your beans by injecting it as managed property @ManagedProperty("#{testClass}") TestClass testClass; Instead of @ManagedProperty you can directly use method FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("testClass"); to retrieve your application scoped managed bean (for session scoped bean use .getSessionMap(). method instead). If you wish so you can additionally initialize TestClass with @PostConstruct annotation.


来源:https://stackoverflow.com/questions/22082098/jsf-2-0-webapplication-value-is-null-after-changed-to-the-next-managed-bean

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