简体   繁体   English

JSF ConversationScoped bean 未在 POST 之间提升 scope

[英]JSF ConversationScoped bean not elevating scope between POSTs

I have a bean with an action method called view() and a field of type MyObject:我有一个带有名为 view() 的操作方法和 MyObject 类型的字段的 bean:

@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.在 screen1.xhtml 上,我使用 primefaces p:dataTable 和 var="obj" 到 output 行和命令按钮来查看用户点击的行的 object。 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.当用户单击其中一行中的 commandButton 时,他们将被带到 page2.xhtml,其中显示有关 obj 的更多详细信息。 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.当我在 view(MyObject selectedObj) 操作方法中时,我立即调用 conversation.begin(),分配 this.obj = selectedObj,然后用户获取 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.但是,当用户单击 page2 上的命令按钮时,它应该重新显示与来自 page1 时发生的 view() 操作调用分配的 obj 不同的信息,因为 scope 已提升为对话。 This is not happening.这没有发生。 The obj field is null in the bean when the scope should have prevented it from being lost.当 scope 应该防止它丢失时, bean 中的 obj 字段是 null。 So when they clicked a commandButton on page2, it gives null pointer exception when the page tries to resolve #{myBean.someProperty}.因此,当他们单击 page2 上的命令按钮时,当页面尝试解析 #{myBean.someProperty} 时,它会给出 null 指针异常。

What am I missing?我错过了什么? Thank you for any help.感谢您的任何帮助。

@ConversationScoped is a feature of CDI, not JSF itself. @ConversationScoped 是 CDI 的一项功能,而不是 JSF 本身。 That means that in order for it to work properly, you must use @ConversationScoped in combination with @javax.inject.Named, not @ManagedBean.这意味着为了使其正常工作,您必须将@ConversationScoped 与@javax.inject.Named 结合使用,而不是@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. CDI 默认不包含在 JSF 2.0 或 2.1 中,因此您还需要添加一个 CDI 实现,如 Weld(参见http://seamframework.org/Weld )和“empty beans.xml”,如焊接中所述文档。

@ConversationScoped is an annotation of CDI . @ConversationScopedCDI的注解。 If you use it you must never use @ManagedBean which is a JSF annotation.如果你使用它,你绝不能使用@ManagedBean这是一个 JSF 注释。 instead you must annotate the bean with @Named .相反,您必须使用@Named注释 bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM