2013-04-25 45 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的重點在於你可以選擇發送給服務器的內容。