会话在提交表单时不会自动传播吗? [英] Conversation not propagated automatically on form submission?
问题描述
我有以下对话范围的支持bean:
I have the following conversation scoped backing 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视图:
And the following 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!!!
是正确的.
When I enter the page I see INFO: Creating TestConversation bean!!!
which is correct.
但是随后我点击h:commandLink
,我看到了:
But then I click on the h:commandLink
and I see:
信息:正在创建TestConversation bean !!!
INFO:调用commandLinkAction
INFO: Creating TestConversation bean!!!
INFO: Invoking commandLinkAction
再次创建了bean,这意味着未传播对话.我认为这与以下内容矛盾:
The bean was created again, which means that the conversation was not propagated. I think this contradicts with the following:
从 docs 引用:>
与呈现JSF视图的请求相关联的长期对话上下文会自动传播到源自该呈现页面的任何面孔请求(JSF表单提交).
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}"/>
,则一切正常.我有误会吗?
If I add this <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
then everything works fine. Do I have a misunderstanding?
P.S如果没有f:param
,则第二次单击commandLink
时效果很好,但第一次却没有:(.
P.S Without the f:param
it works fine when I click on the commandLink
for the second time, but not on the first time:(.
推荐答案
在上一个答案的基础上,肯定是因为TestConversation
bean尚未被构建,直到为该表单自动包含cid
而为时已晚
Building on the previous answer, it's definitely because the TestConversation
bean is not being constructed until it's already too late for the form to include the cid
automatically.
在这种情况下,您正在为视图初始化数据,因此最好将其放在preRenderView事件侦听器中.
In this case, you're initializing data for the view, so it's probably better to put it in a preRenderView event listener instead.
<f:event type="preRenderView" listener="#{testConversation.init}"/>
将此内容放置在facelet模板的早期,例如f:metadata
(通常与f:viewParam
结合使用),然后删除@PostConstruct
批注.这使得init
的调用是显式的,而不是依赖于正在构造的bean的副作用而运行,因为它是在EL表达式中引用的.
Put this early in your facelet template, such as in the f:metadata
(as it's often used in conjunction with f:viewParam
), and remove the @PostConstruct
annotation. That makes the invocation of init
explicit rather than relying on it being run as a side effect of the bean being constructed because it was referenced in an EL expression.
这篇关于会话在提交表单时不会自动传播吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!