Question

I use Pretty Faces and JSF 2.

I have a selectOneMenu to choose a value. I want then to pass the selected value to a second form with a command button to pass then to an another page. I don't want to see the parameter in the URL.

I don't want the url as this : addpartipant.xhtml, or addparticipant.xhtml?study_name=TEST or /Addparticpant/New/TEST.

I want the URL like this only /Addparticipant/New but if I do this , I don't get to retrieve the good parameter value, I'm only retrieving the value set by default in the bean for the parameter, not the page refreshed value.

More disturbing, I have a button who appears when i change the default value in the page.And it works but it looks like the bean value of parameter is not impact and still stay at default.

How should I to that? Thank for help. I tried so listener in ajax call to set the value param in bean but it's actually not working?

/* Form which throws an ajax event when you change the value of the selectOneMenu to add the value as a parameter to a second form */

<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>

/* Second form where the parameter to pass is injected from an ajax call from a move event from the form below */

 <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 Config File :

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

Page Result addParticipant.xhtml:

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

Solution

Prettyfaces always uses POST-REDIRECT-GET pattern to perform the navigation. So your chance is to invoke a managed bean method in your h:commandButton action method instead of returning the navigation result itself and put your parameter in flash scope:

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

And in your managed 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
}

After that, you can retrieve param1 from your destination managed bean. That's for using hidden parameters.

Whenever you want to use standard visible GET parameters, you'll find this post helpful. Using GET parameters involves sending them with the GET request, so your destination page has to catch them with an <f:viewParam> and can keep them into a destination managed bean property. Here you can refer to my previously answered question:

Source page:

<!--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>

Destination page:

<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>

OTHER TIPS

As you said before, another solution could be to simply use forward with the JSF navigation. The parameters are kept hidden stuck to the first navigation url page (for example we could said /Home) . But ok you won't have the /Addparticpant/New/TEST style.

What is the avantage of POST-REDIRECT-GET offered by PrettyFaces ? And f:param is in fact pointless when you use PrettyFaces because of the POST...unless you use GET (which means you don't use PrettyFaces) & you put parameters in URL. I will read your link because i get some problems in fact to retrieve my parameters according to the scopes, the Post/Get Action & the place( In JSF page/ backing bean).

Alternative Solution :  

    <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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top