2012-05-10 45 views
0

我有一個Web應用程序,它在命中按鈕時基本處理用戶的條目,然後接收事務的響應/結果消息,並且應該將顯示在另一個JSF頁面中。如何在響應期間將支持bean傳遞/重定向/派發到JSF頁面

這裏就是我想要做的一個例子:我使用JSF2和primefaces

registration.xhtml - 起點爲交易

RegistrationBean - 後臺bean使用registration.xhtml - 擁有通過registration.xhtml上的commanButton調用的「create」(也處理輸入的數據並假定設置了ResultBean)方法,然後返回導航字符串(至result.xhtml)

result.xhtml - 交易

ResultBean的結果UI - 持有由result.xhtml

我一直在尋找在互聯網上的樣品需要的值,似乎找不到一個。有沒有辦法做到這一點?如果沒有,也許是一種解決方法?我是一個初學者使用這個。任何幫助將不勝感激。提前致謝! 請參閱下面的示例代碼。

registration.xhtml:

<h:form style="position: absolute" id="basicPartyRegistration"> 
<h:panelGroup> 
    <h:commandButton id="createButton1" action="#{partyRegistration.create}" value="Create"> 
    </h:commandButton> 
    <h:button outcome="welcome" value="Main Page" id="mainPageButton" /> 
</h:panelGroup> 
<br /> 
<h:panelGroup> 
    <p:panelGrid columns="2"> 
    <h:outputText value="Receiver:" /> 
    <h:inputText id="receiver" 
     value="#{partyRegistration.partyRegistrationInfo.receiverGLN}" 
     size="15" maxlength="13" /> 
    <h:outputText value="TransmittingData:" /> 
    <h:inputText id="transmittingDataPool" 
     value="#{partyRegistration.partyRegistrationInfo.transmittingDataPool}" 
     size="15" maxlength="13" /> 
    <h:outputText value="PartyData:" /> 
    <h:inputText id="partyData" 
     value="#{partyRegistration.partyRegistrationInfo.partyDataPool}" 
     size="15" maxlength="13" /> 
</p:panelGrid> 

..... 
..... 

RegistrationBean:

@ManagedBean (name = "partyRegistration") 
@viewScoped //Changed to @ConversationScoped 
public class RegistrationBean implements Serializable{ 
    private String receiver 
    private String transmittingData; 
    private String partyDataPool; 
    @ManagedProperty (value = "resultBean") 
    private Result result; 
    // more variables 
    //public getters and setters 

    public String create(){ 
     // do some processing 
     // some magic way to set RESULT bean to be used in the next page 
     return "result"; 
    } 
} 

result.xhtml

<h:form style="position: absolute" id="partyRegistrationResponse"> 
<h:panelGroup> 
<h:button outcome="welcome" value="Main Page" id="mainPageButton" /> 
</h:panelGroup> 
<br/> 
<h:panelGroup> 
<p:panelGrid columns="4"> 
    <h:outputText value="Last Date Changed: " /> 
    <p:inputText id="lastDateChg" value="#{partyResponse.lastChangedDateItem}" 
     title="Last Date Changed" size="15" > 
    </p:inputText> 

</p:panelGrid> 
<h4>Response Identification</h4> 
..... 
..... 

ResultBean:

@ManagedBean (name = "partyResponse") 
@ViewScoped //changed to @ConversationScoped 
public Class ResultBean implements Serializable{ 
    private Date lastChangedDateItem; 
    //more variables 
    //getters and setters 
} 

faces-config.xml中

<navigation-rule> 
    <navigation-case> 
     <from-outcome>result</from-outcome> 
     <to-view-id>/result.xhtml</to-view-id> 
    </navigation-case> 
</navigation-rule> 
+1

對不起,對我來說,目前還不清楚是什麼你要實現的目標。哪裏有問題? (導航規則在JSF 2.0中不是必需的) –

+0

謝謝Matt。我想在這裏實現的是result.xhtml應該包含ResultBean(在createButton中設置)。我需要在result.xhtml頁面 –

回答

1

嘗試創建ResultBean在RegistrationBean的create()方法,並把它放在一個合格的範圍內編程的方法。由於您要離開註冊視圖,因此ViewScope可能不是正確的選擇。

爲了生存重定向,將其放入閃存範圍。

FacesContext.getCurrentInstance().getExternalContext().getFlash().putNow("partyResponse", resultBean); 

您應該看一下對話範圍,這對於存儲屬於一個用例的一系列頁面的bean非常有用。

+0

Hi Frank的響應消息(來自ResultBean進程)。還有一件事,對話範圍在seam或tomcat上嗎?如果我使用它,你知道他們和primefaces之間是否存在兼容性問題嗎? –

+0

它是通過seam引入的,但現在是官方的java標準:CDI。所以你需要一個啓用CDI的容器,但不一定使用接縫。在接縫主頁上尋找焊縫,這是一個CDI實施。你可以在tomcat中使用它,因爲默認情況下,tomcat不提供CDI。 [link](http://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html/environments.html#d0e5221)Primefaces也可以使用它。 – Frank

+0

嗨,弗蘭克!謝謝!我終於明白了。我使用建議的對話範圍,它的工作!我也嘗試了Flash範圍,但它不適用於視圖範圍的bean。 –

0

如果你只有需要簡單的字符串,爲什麼不使用FacesMessage

public String create(){ 
    FacesContext.getCurrentInstance() 
     .addMessage(null, new FacesMessage("Add your message here")); 
    return "result"; 
} 

,並在您result.xhtml使用<h:messages>標籤:

<h4>Response Identification</h4> 
<h:messages/> 

否則,如果你需要比結果的消息,你應該認真考慮過你的設計越來越把ResultBean在更廣泛的範圍內(談話或會話)。

例如與會話範圍的bean保存當前結果:

@ManagedBean 
@SessionScoped 
public Class SessionBean 
    private Result currentResult; 
    // getter and setter 
} 

,並在您註冊的bean:

// inject the session scoped bean 
@ManagedProperty(value="#{sessionBean}") 
private SessionBean sessionBean; 

public String create(){ 
    sessionBean.setCurrentResult(myResult); 
    return "result"; 
} 
+0

謝謝馬特。但是如果我無法填充ResultBean,我認爲這將是一個解決方法。請注意,ResultBean由日期,狀態和一些值列表組成。 –

+0

然後你應該使用更廣泛的範圍。更新了我的答案。 –

相關問題