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.
Replace <h:button>
by
<h:commandButton value="Submit" action="form?faces-redirect=true&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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With