PrettyFaces & JSF 2 : Do we always need to pass pa

2019-07-25 00:02发布

问题:

Do we always need to pass parameters to the URL to retrieve them ?

In my example , I can't get to retrieve my param named 'study'; How should i do that ?

XHTML File Sender:

<h:commandButton  value="Send" type="button" action="pretty:participant">
    <f:param  name="study"  value ="test"  />
</h:commandButton> 

XHTML File Recever:

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

Pretty Config XML :

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

回答1:

If you want to do it using prettyfaces, yes, you must because it's an url rewritting tool, so the params must go into the url. For your code to work, you should use the f:viewParam tag before using it in your receiver page:

<f:metadata>
    <f:viewParam name="study" value="#{destinationBean.study}" />
</f:metadata>

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

However, if you want to get rid of sending the parameter via url, you have the flash scope as an alternative. That, however, involves JSF and not Prettyfaces. If you want to go with this solution I highly encourage you to avoid early versions of Mojarra JSF 2.x, as they are buggy related to that.



回答2:

Yes, you have to store the parameter somewhere in the URL to retrieve it later. You can do this with either a path parameter or a query parameter. Typically you bind the parameter to bean property with your mapping. This way you can retrieve the value in the page using standard EL expressions which are referring to your bean properties.