使用 GET 提交 JSF 表单
如何将表单提交到同一页面并使用 GET 参数?
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,它将填充文本字段,但表单提交本身似乎不起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
替换为它有效地触发 PRG (Post-Redirect-Get),它将在查询字符串中包含
参数。需要注意的是,目标页面必须具有完全相同的
。另一个解决方案是使用纯 HTML
您仍应保留两侧的
。您只需要意识到转换/验证无法以这种形式完成,它们必须通过目标页面上的
执行。另请参阅:
Replace
<h:button>
byIt 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 anid
matching the parameter name and use a plain<input type="submit">
. You can of course also use plain HTML<input type="text">
here.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:
您还可以注册一个
NavigationHandler
来处理“self”等关键字并重定向到当前视图并添加必要的查询参数。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.