2013-04-25 47 views
3

我正在使用JSF2和PrimeFaces,并且每次发出Ajax请求并检查当前的conversation是否是暂时的,它都不是,并且开始新的对话。如何在Ajax请求中使用@ConversationScoped?

当我禁用Ajax请求时,表单自动传递cid并且会话恢复,但是当这是一个Ajax请求时,cid不会自动传递。

在使用@ConversationScoped的Ajax请求时,如何正确地通过cid?为什么这个参数不会自动传递?

回答

1

我不清楚你的用例是什么;但从理论上讲,您可以使用任何给定组件上的<f:attribute/>标签来传递参数。

  1. <f:attribute/>标记添加到组件发起Ajax调用

    <p:commandLink id="aComponent" value="#{bean.val}" action="#{bean.doSomething}"> 
        <f:attribute name="conversationId" value="#{param['cid']}"/>   
    </p:commandLink> 
    
  2. 你可以拉从组件参数图的参数支持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的重点在于你可以选择发送给服务器的内容。