【问题标题】:How to use @ConversationScoped with Ajax requests?如何将 @ConversationScoped 与 Ajax 请求一起使用?
【发布时间】:2013-04-25 15:38:32
【问题描述】:
我正在使用 JSF2 和 PrimeFaces,每次我发出 Ajax 请求并检查当前的 conversation 是否是瞬态的,它不是并且开始新的对话。
当我禁用 Ajax 请求时,表单会自动传递 cid 并恢复对话,但当是 Ajax 请求时,cid 不会自动传递。
在使用带有@ConversationScoped 的Ajax 请求时,如何正确传递cid?为什么这个参数没有自动传递?
【问题讨论】:
标签:
ajax
jsf-2
primefaces
cdi
conversation-scope
【解决方案1】:
我不太清楚您的用例是什么;但理论上,您可以在任何给定组件上使用<f:attribute/> 标签传递参数。
-
将<f:attribute/>标签添加到发起AJAX调用的组件中
<p:commandLink id="aComponent" value="#{bean.val}" action="#{bean.doSomething}">
<f:attribute name="conversationId" value="#{param['cid']}"/>
</p:commandLink>
-
您可以从支持 bean 中组件的参数映射中提取参数:
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot theView = context.getViewRoot();
UIComponent component = theView.findComponent("aComponent");
Integer theConversationId =(Integer) component.getAttributes().get("cid");
这里的关键点是该参数在#{param} 映射中可用(与任何其他GET 参数一样)。之所以不通过ajax自动传输的原因只是:GET参数需要传输完整的HTTP请求。 AJAX 的全部意义在于您可以选择发送到服务器的内容。