0

I have an inputtext in a page page1.xhtml and I want to pass the value the user will enter into a second page page2.xhmtl as a view parameter using a get method. I use an h:button and put as outcome value from the backing bean but it when I navigate to the second page I the parameter is not passed. What's wrong? Is the value not passed to the backing bean before pressing the button and therefore value cannot be read? Is there another way to do it?

page1.xhtml

h:inputText id="q" value="#{QBean.q}"></h:inputText>
<h:button value="Done" outcome="page2?q=#{indexBean.q}">

page2.xhtml

<f:metadata>
       <f:viewParam name="q" value="#{QBean.q}"/>
</f:metadata>

QBean

private String q;

//setter
//getter
user579674
  • 2,159
  • 6
  • 30
  • 40

2 Answers2

2

Your sole functional requirement seems to be that you want a GET form instead of a POST form. In that case, use normal HTML elements, not JSF components.

<form action="page2.xhtml">
    <input name="q" />
    <input type="submit" value="Done" />
</form>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What if i want to also pass that parameter to a backing bean? – user579674 Mar 29 '12 at 10:19
  • Specify it as `` in `page2.xhtml` like as you already did. No changes needs to be done there. – BalusC Mar 29 '12 at 12:14
  • Okay thanks a lot that works indeed.. So if I wanted to use jsf would I be able to do that? I ask to understand what went wrong with my code and it did not work. – user579674 Mar 29 '12 at 15:29
  • The `page2?q=#{indexBean.q}` is set when JSF is about to generate HTML, not when the enduser is about to press the button. – BalusC Mar 29 '12 at 15:46
  • Okay I tried what you suggested but I have another problem. I get redirect in my page without the /faces/ prefix so my page doesn't look good. How do I fix this? – user579674 Apr 11 '12 at 17:16
  • Just change the `
    ` URL accordingly. All of my JSF 2.x targeted answers assume that you've followed the "best practice" of mapping `FacesServlet` on `*.xhtml`.
    – BalusC Apr 11 '12 at 23:02
  • Can you please point me to someplace where I can further study those "best practices"? Also what if I want to later convert my URLs to REST URLs like "/page1/q/test", specifying a URL in the form action won't break this? – user579674 Apr 12 '12 at 16:48
0

You could use the POST-REDIRECT-GET approach and take commandButton instead: <h:commandButton value="Done" action="page2?faces-redirect=true&amp;includeViewParams=true"/>

fischermatte
  • 3,327
  • 4
  • 42
  • 52
  • seems to be a duplicate of [this one](http://stackoverflow.com/questions/4823216/jsf-2-0-form-using-get) – fischermatte Mar 20 '12 at 19:21
  • That seems interesting. Using this I can pass all view parameters from one page to another as I can understand. I'm not sure if this will work for the problem I currently have though. – user579674 Mar 29 '12 at 10:23