jsf - 如何在 h :form submit 期间将查询参数附加到 POST 请求

标签 jsf primefaces get commandbutton

当我单击 <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,没有附加任何获取参数,尽管初始页面可能有它们。因此,它们也不会在表单提交时设置。

要处理这种情况,您可以:

  1. 使用 @ViewScoped bean 在初始访问时保存这些参数的值;
  2. 在您的表单中添加一些隐藏的输入字段,以便它们在表单提交时也被发送或嵌套 <f:param>在你的里面<h:commandButton> ;
  3. 使用 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/

相关文章:

javascript - 从 .txt 文件中获取文本值

JSF1064 "Unable to find or serve resource"对 jsf 2.0 的警告

jsf - 素面 : update dialog content and keep it open

javascript - 隐藏 p :schedule in primefaces 的事件时间

apache - mod_rewrite 删除一个 GET 变量

android - 在 Android Studio 中使用 GET 方法和 Volley 解析 JSON

java - JSF 托管 Bean 性能

jsf - 写入 h :inputText in h:dataTable 中的 map 属性

jsf - 如何使用导航规则进行重定向

验证 primefaces 编辑器