[英]How does JSF 2 ConversationScope work?
I have a JSF facelets page that displays a table of data depending on which page they are viewing. 我有一个JSF facelets页面,该页面根据正在查看的页面显示数据表。 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). 当显示页面1时,我调用
view()
动作方法从数据库获取两个页面的数据,并将其存储为bean的私有成员字段(两个数组)。 I also call conversation.start()
on the injected conversation instance in the view()
method. 我还通过
view()
方法在注入的对话实例上调用了conversation.start()
。
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. 当用户单击“下一个”按钮(
h:commandButton
)转到第2页时,我正在执行next()
方法以更新备用bean使其指向数组2,以便将其内容打印出来。 The problem is, array 2 no longer exists. 问题是,数组2不再存在。 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: 我将在此处粘贴一个示例,我认为它将帮助您理解对话范围的工作原理:
This is the start page of a wizard(Conversation scope is great for wizards) 这是向导的起始页(会话范围非常适合向导)
<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>
This is the second page of the wizard 这是向导的第二页
<!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>
And this is the page where the scope ends 这是范围结束的页面
<!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>
Here the @ConversationScoped backing bean that starts and ends the conversation 在这里,@ 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
询问您是否不懂
NOTE: 注意:
I think but i am not sure at 100% but ConversationScope only works if the backing bean is a CDI bean. 我认为,但我不确定100%,但是ConversationScope仅在支持bean是CDI bean的情况下起作用。 This mean uses the annotation @Named.
这意味着使用注释@Named。 Better double check that.
最好再检查一遍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.