使用 GET 提交 JSF 表单 [英] Submit a JSF form using GET
问题描述
如何将表单提交到同一页面并使用 GET 参数?
How do I submit a form to the same page and use GET parameters?
JSF 页面内容:
<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>
如果我请求页面:form.jsf?item1=foo&item2=bar,它将填充文本字段,但表单提交给自身似乎不起作用.
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
by
Replace <h:button>
by
<h:commandButton value="Submit" action="form?faces-redirect=true&includeViewParams=true"/>
它有效地触发了一个 PRG(重定向后获取),它将在查询字符串中包含
参数.需要注意的是,目标页面必须具有完全相同的
.
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>
.
另一种解决方案是使用纯 HTML 而不是
,给输入元素一个 id
> 匹配参数名称并使用普通的 .您当然也可以在此处使用纯 HTML
.
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.
这篇关于使用 GET 提交 JSF 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!