JSF 2 ConversationScope如何工作?

时间:2011-10-17 00:52:40

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

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

当用户点击“下一步”按钮(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];

1 个答案:

答案 0 :(得分:47)

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

我将在这里粘贴一个小例子,我认为这将有助于您了解对话范围的工作原理:

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

<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。更好地仔细检查一下。