我有以下对话范围的支持 bean:
@Named
@ConversationScoped
public class TestConversation implements Serializable {
private Logger logger = LoggerFactory.getLogger(TestConversation.class);
private List<Integer> numbers;
@Inject
private Conversation conversation;
@PostConstruct
public void init() {
logger.info("Creating TestConversation bean!!!");
numbers = new ArrayList<Integer>();
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
conversation.begin();
}
public void commandLinkAction() {
logger.info("Invoking commandLinkAction");
}
public List<Integer> getNumbers() {
return numbers;
}
}
以及以下 facelets View :
<!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>Testing Conversation</title>
</h:head>
<h:body>
<h:form>
<h:dataTable value="#{testConversation.numbers}" var="num">
<h:column>
<h:outputText value="#{num}"/>
</h:column>
<h:column>
<h:commandLink action="#{testConversation.commandLinkAction}">Trigger form submission</h:commandLink>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
当我进入页面时,我看到 INFO: Creating TestConversation bean!!!
哪个是对的。
然后我点击了 h:commandLink
我看到:
INFO: Creating TestConversation bean!!!
INFO: Invoking commandLinkAction
bean 被再次创建,这意味着对话没有被传播。我认为这与以下内容相矛盾:
引自 docs :
The long-running conversation context associated with a request that renders a JSF view is automatically propagated to any faces request (JSF form submission) that originates from that rendered page.
如果我添加这个 <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
然后一切正常。我有什么误解吗?
P.S 没有 f:param
当我点击 commandLink
时它工作正常第二次,但不是第一次:(。
最佳答案
我想,问题似乎是当 <h:form>
时对话没有开始。组件已处理,因此表单的操作第一次没有包含 cid..
第二次,当你点击一个链接时,testConversation.commandLinkAction
, 访问 testConversation
在处理 <h:form>
之前开始对话
试试下面的改变
如果你放置 #{testConversation}
之前<h:form>
示例工作正常,因为在处理 <h:form>
之前开始对话
希望这有帮助..
关于java - session 不会在表单提交时自动传播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8552478/