Does f:viewParam only pass query string in url when first page uses the same managed bean as the second page?

后端 未结 2 1832
说谎
说谎 2020-12-10 09:21

Let\'s use a search page and a results page for example. If i have a ViewScoped bean that handles my search page and my results page, i\'m able to pass parameters through th

相关标签:
2条回答
  • 2020-12-10 09:50

    I suggest you to implement two @ViewScoped managed beans, one for the search page and the other for the results page. You should also have two xhtml pages, each one related with a bean. Obviusly its page will have its own url (as it seems you're doing right now with the same bean).

    You can make the second page's bean expect two parameters, firstName and lastName. After, in the preRenderView method, when parameters are already set into the second managed bean, query your DB with that values. So how to achieve the transition between the beans? An outcome should be enough.

    <p:button outcome="results">
        <f:param name="firstName" value="#{searchBean.firstName}">
        <f:param name="lastName" value="#{searchBean.lastName}">
    </p:button>
    

    This makes JSF build an url with the params you need. After you can do exactly the same that you're doing right now, but using your second bean to get your params (I strongly suggest you not to use a getter method for the preRenderView method):

    <f:metadata>
      <f:event type="preRenderView" listener="#{resultBacker.initialize}" />
      <f:viewParam name="firstName" value="#{resultBacker.firstName}"/>
      <f:viewParam name="lastName" value="#{resultBacker.lastName}"/>
    </f:metadata>
    
    0 讨论(0)
  • 2020-12-10 10:15

    includeViewParams works only if both the source and target view have the desired view parameters declared as <f:viewParam>.

    So, in your parituclar case, you need to put the <f:viewParam>s with very same name in both the search.xhtml and results.xhtml. Under the covers, the includeViewParams namely only grabs the view params of the current view and then only applies the ones which are also declared in the target view.

    See also:

    • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?

    Unrelated to the concrete problem, you seem to effectively want a GET form. In that case, there is a better way for that than performing a POST-Redirect-GET with parameters. Look at the bottom of the above "See also" link.

    0 讨论(0)
提交回复
热议问题