8

How do I submit a form to the same page and use GET parameters?

JSF page contents:

<f:metadata>
    <f:viewParam name="item1" value="#{bean.item1}"/>
    <f:viewParam name="item2" value="#{bean.item2}"/>
</f:metadata>

...

<h:form>
  <h:inputText value="#{bean.item1}"/>
  <h:inputText value="#{bean.item2}"/>

  <h:button value="Submit" >
      <f:param name="item1" value="#{bean.item1}"/>
      <f:param name="item2" value="#{bean.item2}"/>
  </h:button>
</h:form>

If I request the page: form.jsf?item1=foo&item2=bar, it will populate the text fields, but the form submission to itself doesn't seem to work.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
monkey_wrench
  • 81
  • 1
  • 2

2 Answers2

15

Replace <h:button> by

<h:commandButton value="Submit" action="form?faces-redirect=true&amp;includeViewParams=true"/>

It effectively fires a PRG (Post-Redirect-Get) which will include the <f:viewParam> params in the query string. Noted should be that the target page must have exactly same <f:viewParam>.

Another solution is to use a plain HTML <form> instead of <h:form>, give the input elements an id matching the parameter name and use a plain <input type="submit">. You can of course also use plain HTML <input type="text"> here.

<form>
    <h:inputText id="item1" value="#{bean.item1}"/>
    <h:inputText id="item2" value="#{bean.item2}"/>

    <input type="submit" value="Submit" />
</form>

You should still keep the <f:viewParam> in both sides. You only need to realize that conversion/validation couldn't be done in this form, they have to be performed via the <f:viewParam> on the target page.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You could also register a NavigationHandler that handles a keyword like 'self' and redirects to the the current view and adds the necessary query parameters.

djmj
  • 5,579
  • 5
  • 54
  • 92