我发布了类似的问题。看看here。我解决了在托管bean之间共享值的问题。我的新问题是分享我在一个类中的一个mangedbean中设置的值。并希望在antoher manged bean中获得此值。这是解释我的问题的代码示例。
@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;
}
}
但我想要TestClass中的值。
有了它,您可以在会话映射中放置和获取自定义对象
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);
}
}