Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit a JSF form using GET

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.

like image 735
monkey_wrench Avatar asked Jan 27 '11 23:01

monkey_wrench


1 Answers

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:

  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
like image 77
BalusC Avatar answered Nov 13 '22 16:11

BalusC