JSF 2 ConversationScope 如何工作? [英] How does JSF 2 ConversationScope work?
问题描述
我有一个 JSF facelets 页面,根据他们正在查看的页面显示数据表.当我显示第 1 页时,我调用 view()
操作方法从数据库中获取两个页面的数据并将其存储为 bean 的私有成员字段(两个数组).我还在 view()
方法中对注入的对话实例调用 conversation.start()
.
I have a JSF facelets page that displays a table of data depending on which page they are viewing. When I display page 1, I call the view()
action method to get the data from the database for both pages and store it as a private member field of the bean (two arrays). I also call conversation.start()
on the injected conversation instance in the view()
method.
当用户单击下一步"按钮 (h:commandButton
) 转到第 2 页时,我正在执行 next()
方法来更新支持 bean指向数组 2,这样它就会打印出它的内容.问题是,数组 2 不再存在.我不知道为什么我失去了对话范围.有什么想法吗?
When the user clicks the "next" button (h:commandButton
) to go to page 2, I am executing a next()
method to update the backing bean to point to array 2 so it will print out its contents. The problem is, array 2 no longer exists. I don't know why I am losing conversation scope. Any ideas?
//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];
推荐答案
您应该粘贴更多代码,以便我们更好地帮助您.从你说的我看不出你在哪里调用结束对话的方法(在使用对话范围时你也需要它).
You should paste some more code so we can help you better. From what you say i cannot see where you called the method for ending the conversation(You need that too when working with conversation scope).
我将在此处粘贴一个我认为可以帮助您了解对话范围的工作原理的小示例:
I will paste here a little example that i think will help you understand how conversation scope works:
这是向导的起始页(对话范围非常适合向导)
<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;
}
}
我觉得这个例子很简单,可以帮助你理解它是如何工作的.有不懂的就问
I think this example is very simple and can help you understand how it works. Ask if you don't understand something
注意:
我认为但我不确定是否为 100%,但 ConversationScope 仅在支持 bean 是 CDI bean 时才有效.这意味着使用注释@Named.最好仔细检查一下.
I think but i am not sure at 100% but ConversationScope only works if the backing bean is a CDI bean. This mean uses the annotation @Named. Better double check that.
这篇关于JSF 2 ConversationScope 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!