JSF ConversationScoped bean 未提升 POST 之间的范围
我有一个带有名为 view() 的操作方法的 bean 和一个 MyObject 类型的字段:
@ManagedBean
@ConversationScoped
public class MyBean implements Serializable{
private @Inject Conversation conversation; //has getter and setter
private MyObject object; //has getter and setter
... other fields follow
public String view(MyObject selectedObj){
conversation.begin();
return "success";
}
public String getSomeProperty(){
return object.getProperty();
}
...other methods follow
}
在 screen1.xhtml 上,我使用 primefaces p:dataTable 和 var="obj" 来输出带有 commandButton 的行,以查看该行的对象用户点击。每行上的按钮如下所示。
<p:commandButton action="#{myBean.view(obj)}"
ajax="false" title="View Clone" image="ui-icon ui-icon-locked"/>
当用户单击其中一行中的命令按钮时,他们将被带到 page2.xhtml,其中显示有关 obj 的更多详细信息。这可以正常工作并显示详细信息。当我在 view(MyObject selectedObj) 操作方法内部时,我立即调用 Conversation.begin(),分配 this.obj = selectedObj,然后用户获取 page2.xhtml。
但是,当用户单击第 2 页上的命令按钮时,它应该重新显示与来自第 1 页时发生的 view() 操作调用分配的 obj 不同的信息,因为范围已提升为对话。这并没有发生。当范围本应防止丢失时,bean 中的 obj 字段为 null。因此,当他们单击第 2 页上的命令按钮时,当页面尝试解析 #{myBean.someProperty} 时,它会给出空指针异常。
我缺少什么?感谢您的帮助。
I have a bean with an action method called view() and a field of type MyObject:
@ManagedBean
@ConversationScoped
public class MyBean implements Serializable{
private @Inject Conversation conversation; //has getter and setter
private MyObject object; //has getter and setter
... other fields follow
public String view(MyObject selectedObj){
conversation.begin();
return "success";
}
public String getSomeProperty(){
return object.getProperty();
}
...other methods follow
}
On screen1.xhtml I am using primefaces p:dataTable with var="obj" to output rows with a commandButton to view the object of the row the user clicks on. The button on each row looks like the following.
<p:commandButton action="#{myBean.view(obj)}"
ajax="false" title="View Clone" image="ui-icon ui-icon-locked"/>
When the user clicks on a commandButton in one of the rows, they are taken to page2.xhtml where more detailed info about the obj is displayed. This works correctly and displays the details. When I am inside of the view(MyObject selectedObj) action method, I immediately call conversation.begin(), assign this.obj = selectedObj, and user gets page2.xhtml.
However, when the user clicks a commandButton on page2 it should redisplay with different information from the obj that was assigned from the view() action call that happened when they came from page1 because the scope was elevated to conversation. This is not happening. The obj field is null in the bean when the scope should have prevented it from being lost. So when they clicked a commandButton on page2, it gives null pointer exception when the page tries to resolve #{myBean.someProperty}.
What am I missing? Thank you for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@ConversationScoped 是 CDI 的一项功能,而不是 JSF 本身。这意味着为了使其正常工作,您必须将 @ConversationScoped 与 @javax.inject.Named 结合使用,而不是 @ManagedBean。
默认情况下,JSF 2.0 或 2.1 中不包含 CDI,因此您还需要添加像 Weld 这样的 CDI 实现(请参阅 http:// /seamframework.org/Weld)和“空 beans.xml”,如焊接文档中所述。
@ConversationScoped is a feature of CDI, not JSF itself. That means that in order for it to work properly, you must use @ConversationScoped in combination with @javax.inject.Named, not @ManagedBean.
CDI is not by default included in JSF 2.0 or 2.1, so you would also need to add a CDI implementation like Weld (see http://seamframework.org/Weld) and an "empty beans.xml", as described in the weld documentation.
@ConversationScoped
是CDI 的注释。如果您使用它,则绝对不能使用@ManagedBean
,它是一个 JSF 注释。相反,您必须使用@Named
注释该 bean。@ConversationScoped
is an annotation of CDI. If you use it you must never use@ManagedBean
which is a JSF annotation. instead you must annotate the bean with@Named
.