Submit a JSF form using GET
Asked Answered
R

2

9

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.

Replica answered 27/1, 2011 at 23:33 Comment(0)
S
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:

Studdingsail answered 27/1, 2011 at 23:46 Comment(0)
E
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.

Ethnography answered 26/3, 2013 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.