Jsf 2 JSF2视图范围状态保存无法正确保存注入的属性和ejb
我目前正在使用WebSphere8.5和JSF2.0(MyFaces)。由于@ViewScoped bean的状态保存(服务器和客户端模式),我一直面临一些问题。我有下面的豆子Jsf 2 JSF2视图范围状态保存无法正确保存注入的属性和ejb,jsf-2,ejb,websphere,view-scope,managed-property,Jsf 2,Ejb,Websphere,View Scope,Managed Property,我目前正在使用WebSphere8.5和JSF2.0(MyFaces)。由于@ViewScoped bean的状态保存(服务器和客户端模式),我一直面临一些问题。我有下面的豆子 @ManagedBean @ViewScoped public class BrokenBean implements Serializable { //Problem: not synchronized with real SessionBean object @ManagedProperty(val
@ManagedBean
@ViewScoped
public class BrokenBean implements Serializable {
//Problem: not synchronized with real SessionBean object
@ManagedProperty(value="#{sessionBean})
private SessionBean session;
//Problem: this gives NotSerializableException to some server generated class.
@EJB
private MyEJB myejb;
//Getter and Setter for above properties
}
@ManagedBean
@SessionScoped
public class SessionBean implements serializable{
private Integer i;
//Getter and Setter
}
//MyEJB has a method to get Counter from database
我的测试页面包含
<h:outputText value="#{brokenBean.session.i}"></h:outputText>
<h:outputText value="#{brokenBean.myejb.getCounter()}"></h:outputText>
从您的问题中可以看出,
{brokenBean.myejb.getCounter()}
应该被引用为{brokenBean.myejb.counter()}
,因为您不需要为EL中的getter/setter方法指定前缀。@XtremeBiker哦,我不知道方法名“getCounter()”看起来像真正的“getter”。在我的测试应用程序中,我将一个数据库访问方法命名为“getCounter()”,但实际上没有名为counter的属性,所以我认为应该将它放在那里。
@ManagedBean
@ViewScoped
public class FixedBrokenBean implements Serializable {
private transient SessionBean session;
private transient MyEJB myejb;
//I must use getters below when I access these two properties within this class
//so that getters will perform a lookup if they are null at the time I need them.
public MyEJB getMyejb() {
if (myejb == null) {
try {
InitialContext ic = new InitialContext();
myejb = (MyEJB) ic.lookup("java:global/testear/testejb/MyEJB !test.MyEJB");
} catch (NamingException e) {
e.printStackTrace();
}
}
return myejb;
}
public SessionBean getSession() {
if (session == null) {
FacesContext context = FacesContext.getCurrentInstance();
session = (SessionBean) context.getApplication().evaluateExpressionGet(context, "#{sessionBean}", SessionBean.class);
}
return session;
}
}