问题
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.
回答1:
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.
See also:
- What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
回答2:
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.
来源:https://stackoverflow.com/questions/4823216/submit-a-jsf-form-using-get