Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

jsf - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...