2

I want to navigate from one page to another page, say from page1 with bean1 to page2 with bean2. I need to pass some parameters from bean1 to bean2.

I would like to understand how to use @ManagedProperty for parameters and <f:viewParam> in <f:metadata> portion of page2. Say, I have field1, field2 and field3 available in bean1 and bean2 with getters and setters. My understanding is that I will have to define view params in Metadata of page2:

Like

<f:metadata>
    <f:viewParam name="field1" value="#{bean2.field1}"/>
    <f:viewParam name="field2" value="#{bean2.field2}"/>
    <f:viewParam name="field3" value="#{bean2.field3}"/>
</f:metadata> 

I am not sure where I use annotations for @ManagedProperty to define the parameters field1, field2 and field3, in bean1 or bean2.

On page1 I can use "page2?faces-redirect=true&amp;includeViewParams=true"

Can I use the same in one of my methods instead in page1, say on response to a submit of commandlink?

If I need to have those three fields in both page1 and page2, can I define those hidden fields?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Peter
  • 131
  • 1
  • 14

1 Answers1

4

You need to specify them as <f:param> in the <h:link> of page1.xhtml.

<h:link value="Go to page2" outcome="page2">
    <f:param name="field1" value="#{bean1.field1}" />
    <f:param name="field2" value="#{bean1.field2}" />
    <f:param name="field3" value="#{bean1.field3}" />
</h:link>

You can then use <f:viewParam> (or @ManagedProperty, but this allows less fine grained validation) to set them in bean of page2.xhtml.

<f:metadata>
    <f:viewParam name="field1" value="#{bean2.field1}" />
    <f:viewParam name="field2" value="#{bean2.field2}" />
    <f:viewParam name="field3" value="#{bean2.field3}" />
</f:metadata>

You do not need to send a POST request by <h:commandLink> with faces-redirect and includeViewParam here. Just a simple GET request by <h:link> is much simpler and SEO friendlier.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you so much BalusC. I really appreciate. I have gone through quite few articales of yours and I just want to say that I love those. You explain so well. – Peter Mar 20 '12 at 02:55
  • Sorry just hit the Enter key. I was hoping that you happen to see my post and my wish was granted. Thank you. Here is what I am trying to do. I have a datatable with selectionmode single and on ajax event rowselect, I would like to navigate to another page to show the details (another table) for the selected row. so in my method below for onRowSelect I am navigating to another page. – Peter Mar 20 '12 at 03:11
  • 'public String onRowSelect(SelectEvent event) { ConfigurableNavigationHandler configurableNavigationHandler = (ConfigurableNavigationHandler)FacesContext. getCurrentInstance().getApplication().getNavigationHandler(); configurableNavigationHandler.performNavigation("DetailBean?faces-redirect=true"); return "";' Here I would like to add '&includeViewParams=true' I am using primefaces ' – Peter Mar 20 '12 at 03:18
  • You're welcome. Sorry, this is offtopic. Ask a new and clear question :) – BalusC Mar 20 '12 at 03:20
  • Sure, I can do that but I am still trying to understand or say learn how I can pass parameters when I naviagte to another page using url in a method. I can post another question if you want but please confirm. Thank you so much. – Peter Mar 20 '12 at 03:29
  • Hi BalusC, I have posted another question regarding parameters passing onRowSelect. The matter says "How to navigate to another page on onRowSelect" Thanks! – Peter Mar 20 '12 at 04:09
  • Thanks BalusC. I did not see any link yo mark a question answered so just clicked on the tick mark which turned to green. Please let me know if there is anything else needs to be done. I have visited the link that you posted. Thanks again, BalusC. – Peter Mar 20 '12 at 11:49
  • Balusc, Trust all is well on your side. I was trying to psot this simple question, tried ten times at least but ir won't let me put so I thought of adding it here and asking you directly. This is what I was trying to post. – Peter Mar 23 '12 at 03:49
  • What is the correct way to set object variable bfield in the follwoing test class? public class test { private String afield; private String bfield; public test() { buildList(); } public void buildList() { some code to derive and populate afield. this.bfield = this.afield; // ( 1) setBfield(afield); // (2) say getter and setters do exist bfield = afield; // (3) } What is the right way to do? Is option 1 OK or option 2? – Peter Mar 23 '12 at 03:51
  • Did you get a particular error message when you try to post a new question? Try searching http://meta.stackoverflow.com on the exact error message. Contact team@stackoverflow.com if you stucks. – BalusC Mar 23 '12 at 03:58
  • Sure, will do so. I think it was about the contents, sort of not asking in details my question. I tried to add a line here and there to make that big but was gettiong teh same message.- Peter – Peter Mar 23 '12 at 04:00
  • I just added my question. I just need the answer from you. Thanks, – Peter Mar 23 '12 at 04:02
  • BalusC, I just posted my question. I know it is a very basic from Java but would like to get it cleared. I hope you won't mind. – Peter Mar 23 '12 at 04:27