【问题标题】:How does JSF 2 ConversationScope work?JSF 2 ConversationScope 是如何工作的?
【发布时间】:2011-12-08 23:03:21
【问题描述】:

我有一个 JSF facelets 页面,该页面根据他们正在查看的页面显示数据表。当我显示第 1 页时,我调用 view() 操作方法从数据库中获取两个页面的数据并将其存储为 bean 的私有成员字段(两个数组)。我还在view() 方法中对注入的会话实例调用conversation.start()

当用户单击“下一步”按钮 (h:commandButton) 转到第 2 页时,我正在执行 next() 方法来更新支持 bean 以指向数组 2,以便打印出其内容。问题是,数组 2 不再存在。我不知道为什么我失去了谈话范围。有什么想法吗?

//tells the object which page we are on, and thus what data to display.
private int part = 1; 

// These arrays are filled with data but conversation scope doesn't 
// keep them on the next postback.
private int[] part1 = new int[15], part2 = new int[15];

【问题讨论】:

    标签: jsf jsf-2 seam-conversation conversation-scope


    【解决方案1】:

    您应该粘贴更多代码,以便我们更好地为您提供帮助。 从你所说的我看不出你在哪里调用了结束对话的方法(在使用对话范围时你也需要它)。

    我将在此处粘贴一个我认为可以帮助您了解对话范围如何工作的小示例:

    这是向导的起始页(对话范围非常适合向导)

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core">
    
    <h:head>
        <title>ConversationScoped demo CDI(Component Dependency
        Injection)</title>
    </h:head>
    
    <h:body>
    
    
    
        <h3>ConversationScoped demo CDI(Component Dependency Injection)</h3>
    
        <p>A conversation scope provides persistence until a goal is
        reached<br />
        In this example the first entered value will remain until the end
        method is called<br />
        in some page.<br />
        This is a really useful gadget, for making registration wizards and
        similar tools...</p>
    
        <h:form>
            <h:outputText value="Type something" />
            <h:inputText value="#{ supportBB.someValue}" />
            <h:commandButton value="continue" action="#{ supportBB.onClick}" />
        </h:form>
    
    </h:body>
    </html>
    

    这是向导的第二页

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core">
    
    <h:head>
        <title>ConversationScoped demo CDI(Component Dependency
        Injection)</title>
    </h:head>
    
    <h:body>
    
    
    
        <h3>This is the next page(The value is saved in the conversation)</h3>
    
            <h4><h:outputText value="#{ supportBB.someValue}"/></h4>
    
        <h:form>        
            <h:commandButton value="Finish conversation" action="#{ supportBB.onKeepGoing}"/>
        </h:form>
    
    </h:body>
    </html>
    

    这是范围结束的页面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core">
    
    <h:head>
        <title>ConversationScoped demo CDI(Component Dependency
        Injection)</title>
    </h:head>
    
    <h:body>
    
    
    
        <h3>This is the last page.The value still saved in conversation(until the end() method is called)</h3>
    
        <h4><h:outputText value="#{ supportBB.someValue}" /></h4>
    
        <h:form>
            <h:outputText
                value="Click finish to end the conversation(So saved values are disposed)" />
            <h:commandButton value="Finish" action="#{ supportBB.onFinish}" />
        </h:form>
    
    </h:body>
    </html>
    

    这里是启动和结束对话的 @ConversationScoped 支持 bean

    package backingbeans;
    
    import java.io.Serializable;
    
    import javax.enterprise.context.Conversation;
    import javax.enterprise.context.ConversationScoped;
    import javax.inject.Inject;
    import javax.inject.Named;
    
    @Named()
    @ConversationScoped()
    public class SupportBB implements Serializable {
        private static final long serialVersionUID = 1L;
        private String someValue;
        @Inject
        private Conversation conversation;
    
        // Control start and end of conversation
        public void start() {
            conversation.begin();
        }
    
        public void end() {
            conversation.end();
        }
    
        // Navigation
        public String onClick() {
            if(someValue.equals("") || conversation == null) {
                return "";//Dont go anywhere if the there was no input the field
            }
            start();
            return "nextpage?faces-redirect=true";
        }
    
    public String onKeepGoing() {
        return "finish?faces-redirect=true";
    }
    
    public String onFinish() {
        end();// If triggered when there is no conversation(i.e URL Navigation)
                // there will be an exception
        return "index?faces-redirect=true";
    }
    
    // Getters & Setters
    public String getSomeValue() {
        return someValue;
    }
    
    public void setSomeValue(String someValue) {
        this.someValue = someValue;
    }
    
    }
    

    我认为这个例子很简单,可以帮助你理解它是如何工作的。有什么不懂的问一下

    注意:

    我认为,但我不确定 100%,但 ConversationScope 仅在支持 bean 是 CDI bean 时才有效。这意味着使用注释@Named。最好仔细检查一下。

    【讨论】:

    • 感谢您的回复。当我今晚回到我的另一台电脑时,我将不得不试一试。我需要重定向还是只传递页面?
    • @Adam Fisher 你可以通过页面,但我总是喜欢使用重定向,只是为了确保:)
    • 感谢您提供详细的示例。我的问题是在范围内使用 @ManagedBean 而不是 @Named()。
    • 示例中为什么会有这一行? private static final long serialVersionUID = 1L;
    • @Brian 抱歉,这与此答案无关,我从我的 IDE 复制粘贴。
    猜你喜欢
    • 2012-06-12
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 2018-02-10
    • 2017-12-08
    相关资源
    最近更新 更多