1

I have post i similar question. Look here. I solved the problem to share values between managed beans. My new problem ist to share the values i set in one mangedbean in a class. And want to get this value in antoher manged bean. Here is the code example to explain my question.

@ManagedBean
@SessionScoped
public class Example{

   String hello = "hello";
   private TestClass test = new TestClass();
   String myVarBean = hello;

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

  public String getMyVarBean(){
     return myVarBean;
  }
}


@ManagedBean
@SessionScoped
public class Example2{
   @ManagedProperty(value = "#{Example}")
 private Example example;
   String testString;
   private TestClass test = new TestClass();

   public after(){
      String testInt = test.getmyVar(); // I get null because it is antoher instance
      String testInt = example.getMyVarBean(); // i get "hello"
   }
   public void setExample(Example example){
       this.example = example;}
}


public class TestClass{
   private String myVar;

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

   public String getMyVar(){
      return myVar;
   }
}

But I want the values from the TestClass.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
marius0114
  • 145
  • 4
  • 18

1 Answers1

0

With this you can put and get custom object to and from the session map

public final class FacesManager {

    public void putObjectIntoSessionMap(final String key, final Object value) {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                .put(key, value);
    }

    public Object getObjectFromSessionMap(final String key) {
        return FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().get(key);
    }
}
My-Name-Is
  • 4,814
  • 10
  • 44
  • 84
  • Thank this works! But i didn't need this in the final version. I put the variable i created in the manged bean not in a view class, but in a manged bean class. – marius0114 Mar 01 '14 at 17:45