Ada*_*dam 20 jsf seam-conversation jsf-2 conversation-scope
我有一个JSF facelets页面,根据他们正在查看的页面显示数据表.当我显示第1页时,我调用view()
action方法从数据库获取两个页面的数据,并将其存储为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];
Run Code Online (Sandbox Code Playgroud)
sfr*_*frj 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>
Run Code Online (Sandbox Code Playgroud)
这是向导的第二页
<!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>
Run Code Online (Sandbox Code Playgroud)
这是范围结束的页面
<!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>
Run Code Online (Sandbox Code Playgroud)
这里是启动和结束对话的@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这个例子很简单,可以帮助你理解它是如何工作的.问你是否不明白
注意:
我想但是我不确定100%但ConversationScope只有在支持bean是CDI bean的情况下才有效.这意味着使用注释@Named.更好的仔细检查一下.
归档时间: |
|
查看次数: |
34419 次 |
最近记录: |