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

混江龙づ霸主 提交于 2019-11-29 22:49:49

问题


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 the url using something like this:

search.xhtml

<p:commandButton value="Search" type="submit" action="#{searchBacker.search}" >

backing bean

@ManagedBean(name="search")
@ViewScoped
public class searchBacker extends AbstractBacking {

  private String firstName = null;
  private String lastName = null;
  private String results = null;

  public String search() {
    return "results?faces-redirect=true&amp;includeViewParams=true";
  }

  public void getResults() {
    MyDAO dao = new MyDAO();
    results = dao.getResults(firstName, lastName);
  }

  //getters and setters
}

results.xhtml

<f:metadata>
  <f:event type="preRenderView" listener="#{searchBacker.getResults}" />
  <f:viewParam name="firstName" value="#{searchBacker.firstName}"/>
  <f:viewParam name="lastName" value="#{searchBacker.lastName}"/>
</f:metadata>

Now lets say i have two managed beans - one for the search page and one for the results page.

Will the query string still be built in the url with 2 different managed beans or does this only work when using the same managed bean for both pages?

UPDATE

I have the same <f:viewParam> on my search.xhtml and results.xhtml page, but the only difference is that my f:viewParam value points to a different backer in my results.xhtml than in my search.xhtml. When i do this, no params get passed through the url. When I point my f:viewParam value in results.xhtml to the same backer that i use in search.xhtml, the param gets passed through the url just fine, but the value doesn't exist in the results backer where i need it. If i have duplicate f:viewParams in my results.xhtml page - one with the search backer and one with the results backer, everything works fine. Is having 2 of the same f:viewParams with both managed beans the correct way to do this?

Examples:

results.xhtml - params get passed through url, but are not available in my resultsBacker

<f:metadata>
  <f:viewParam name="firstName" value="#{searchBacker.firstName}"/>
  <f:viewParam name="lastName" value="#{searchBacker.lastName}"/>
</f:metadata>

results.xhtml - no params get passed through url

<f:metadata>
  <f:viewParam name="firstName" value="#{resultsBacker.firstName}"/>
  <f:viewParam name="lastName" value="#{resultsBacker.lastName}"/>
</f:metadata>

results.xhtml - params get passed through url and are available in my resultsBacker, but seems clunky. Is this the correct way to do this or am i still missing something?

<f:metadata>
  <f:viewParam name="firstName" value="#{searchBacker.firstName}"/>
  <f:viewParam name="firstName" value="#{resultsBacker.firstName}"/>
  <f:viewParam name="lastName" value="#{searchBacker.lastName}"/>
  <f:viewParam name="lastName" value="#{resultsBacker.lastName}"/>
</f:metadata>

回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/18025508/does-fviewparam-only-pass-query-string-in-url-when-first-page-uses-the-same-man

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!