当我单击 <h:form>
的命令按钮时在下面提到的页面中标记,我希望一旦我点击提交按钮,查询参数就会作为请求参数发送,因为我的页面 URL 是 ../index.xhtml?cid=12&ctype=video
.
我希望在我的操作方法中打印 CID=12
,但是,它打印了 CID=NULL
.问题是什么,我该如何解决?
我的看法:
<h:body id="body">
<h:form id="form">
<p:commandButton action="#{authorizationBean.getParameters()}" value="Ajax Submit" id="ajax" />
</h:form>
</h:body>
我的托管 bean:
@ManagedBean
@SessionScoped
public class AuthorizationBean {
public boolean getParameters(){
Map<String, String> parameterMap = (Map<String, String>) FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap();
String cid = parameterMap.get("cid");
System.out.println("CID="+cid);
return true;
}
}
最佳答案
默认情况下,您的代码(尤其是您的 <h:form>
标记)会生成以下 HTML 输出:
<form id="form" name="form" method="post" action="/yourApp/yourPage.xhtml" enctype="application/x-www-form-urlencoded">
<input type="submit" name="j..." value="Ajax Submit" />
<input id="javax.faces.ViewState" ... />
</form>
请注意 action
生成的 <form>
element 是当前 View ID,没有附加任何获取参数,尽管初始页面可能有它们。因此,它们也不会在表单提交时设置。
要处理这种情况,您可以:
- 使用
@ViewScoped
bean 在初始访问时保存这些参数的值; - 在您的表单中添加一些隐藏的输入字段,以便它们在表单提交时也被发送或嵌套
<f:param>
在你的里面<h:commandButton>
; - 使用 OmniFaces 的
<o:form includeViewParams="true">
标记 ( Tag documentation/Showcase example ) 而不是<h:form>
, 因为它将提交给当前 URL 并附加 View 参数,前提是它们是使用<f:viewParam>
设置的(例如,有关详细信息,请参阅 BalusC 对 Retain original GET request parameters across postbacks 的回答)。
关于jsf - 如何在 h :form submit 期间将查询参数附加到 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19118062/