gpt4 book ai didi

jsf - JSF 2 ConversationScope 是如何工作的?

转载 作者:行者123 更新时间:2023-12-03 12:04:30 25 4
gpt4 key购买 nike

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

当用户单击“下一步”按钮 (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。最好仔细检查一下。

关于jsf - JSF 2 ConversationScope 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7788430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com