Search code examples
jsfjsf-2view-scope

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


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>

Solution

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


    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.