繁体   English   中英

JSF 2 ConversationScope如何工作?

[英]How does JSF 2 ConversationScope work?

我有一个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];

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

我将在此处粘贴一个示例,我认为它将帮助您理解对话范围的工作原理:

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

<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。 最好再检查一遍。

暂无
暂无

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

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