簡體   English   中英

JSF 2:如何從SelectOneMenu將值刷新到支持bean中?

[英]JSF 2 : How to refresh value into the backing bean from a SelectOneMenu?

我使用Pretty Faces和JSF 2。

我有一個selectOneMenu選擇一個值。 然后,我想使用命令按鈕將選定的值傳遞給第二種形式,然后傳遞給另一個頁面。 我不想在URL中看到參數。

我不希望這樣的網址:addpartipant.xhtml或addparticipant.xhtml?study_name = TEST或/ Addparticpant / New / TEST。

我只想要這樣的URL / Addparticipant / New,但是如果執行此操作,則無法獲取好的參數值,我只是在Bean中檢索默認設置的參數值,而不刷新頁面值。

更令人不安的是,當我更改頁面中的默認值時,我會看到一個按鈕出現。它可以正常工作,但看起來參數的bean值不受影響,仍然保持默認值。

我該怎么辦? 感謝您的幫助。 我在ajax調用中試過讓監聽器在bean中設置值param,但實際上不起作用嗎?

/ *當您更改selectOneMenu的值以將該值作為參數添加到第二個表單時,拋出ajax事件的表單* /

<h:form id="studySelection_form">
  <h:outputText value="Sélection de l'étude :"/>
  <h:selectOneMenu  id="study" 
                    value="#{home.study_name}" 
                    required="true"
                    requiredMesage="Selectionnez au moins une étude.">
    <f:selectItems value="#{home.studyNameItems}" 
                   var="c" 
                   itemLabel="#{c.studyNameLabel}" 
                   itemValue="#{c.studyNameValue}" />

    <f:ajax execute="study" 
            render=":add_form" 
            event="change" 
            listener="#{home.updateStudyname}"/>
   </h:selectOneMenu>
 </h:form>

/ *第二種形式,其中要傳遞的參數是從以下形式的移動事件的ajax調用中注入的,該形式來自下面的形式* /

 <h:commandButton value="Ajout" 
                  type="button" 
                  action="pretty:participant" 
                  rendered="#{home.study_name ne '-----'}" >
   <f:param name="theNameofTheStudy" 
            value="#{home.study_name}"  />
 </h:commandButton>

PrettyFaces配置文件:

<url-mapping id="participant">
  <pattern value="/Participant/New/" />
  <view-id value="/addParticipant.xhtml" />
</url-mapping>

頁面結果addParticipant.xhtml:

<h:outputFormat value="Print : {0}, {1} ">
  <f:param value="#{home.study_name}" />
  <f:param value=" #{theNameofTheStudy}" />
</h:outputFormat>

Prettyfaces 始終使用POST-REDIRECT-GET模式執行導航。 因此,您的機會是在h:commandButton操作方法中調用托管Bean方法,而不是返回導航結果本身並將參數放入Flash作用域中

<h:commandButton value="Ajout" 
    action="#{home.actionGoToAddStudent}" 
    rendered="#{home.study_name ne '-----'}" />

在您的托管bean中:

public String actionGoToAddStudent(){
    FacesContext.getCurrentInstance().getExternalContext().getFlash().put("param1", study_name);
    return "pretty:participant"; //or just "/addParticipant.xhtml" and Prettyfaces will translate to pretty url
}

之后,您可以從目標托管Bean中檢索param1 那是使用隱藏參數。

每當您想使用標准的可見GET參數時,都會發現此文章有幫助。 使用GET參數涉及將它們與GET請求一起發送,因此目標頁面必須使用<f:viewParam>捕獲它們,並將其保留在目標托管Bean屬性中。 在這里您可以參考我之前回答的問題

來源頁面:

<!--Performs a GET request with study param-->
<h:button value="Basic GET request" outcome="pretty:participant">
    <f:param name="study" value="#{home.study}" />
</h:button>

目標頁面:

<f:metadata>
    <!--Takes the study param and sets it into the destination bean's property-->
    <f:viewParam name="study" value="#{destinationBean.study}" />
</f:metadata>

<h:outputFormat value="Print : {0} ">
   <f:param value="#{destinationBean.study}" />      
</h:outputFormat>

如您之前所說,另一種解決方案可能是簡單地將前進與JSF導航一起使用。 參數被隱藏隱藏在第一個導航URL頁面上(例如,我們可以說/ Home)。 但是好吧,您將不會使用/ Addparticpant / New / TEST樣式。

PrettyFaces提供的POST-REDIRECT-GET的優點是什么? f:param實際上由於POST而在使用PrettyFaces時毫無意義...除非您使用GET(這意味着您不使用PrettyFaces)並且將參數放在URL中。 我將閱讀您的鏈接,因為實際上在根據范圍,Post / Get Action和place(在JSF頁面/后備bean中)檢索參數時遇到一些問題。

替代解決方案:

    <navigation-rule>
       <from-view-id>/home.xhtml</from-view-id> 
       <navigation-case>
          <from-outcome>participant</from-outcome>
          <to-view-id>/addParticipant.xhtml</to-view-id>
       </navigation-case>
    </navigation-rule>


    /*I use viewparam but if I don't use it it's working too, I clearly don't understand what viewparam is used for  */

    <f:metadata>
          <f:viewParam name="study_name" value="#{home.study_name}" required="true"/>
     </f:metadata>

   /* What is the best way to retrieve information? Then how i load param in a backing bean who this destination page ? */ 
   <h:outputFormat value="Hello,{0},{1},">
                <f:param value="#{home.study_name}" />
                <f:param value="#{param.studynameItem}" />
    </h:outputFormat>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM