【问题标题】:How to attach query parameters to a POST request during h:form submit如何在 h:form 提交期间将查询参数附加到 POST 请求
【发布时间】:2013-10-01 13:53:41
【问题描述】:

当我点击下面提到的页面中<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;
    }

}

【问题讨论】:

    标签: jsf primefaces get commandbutton


    【解决方案1】:

    默认情况下,您的代码(尤其是您的 &lt;h:form&gt; 标记)会生成以下 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>
    

    请注意,生成的&lt;form&gt; 元素的action 是当前视图ID,没有附加任何获取参数,尽管初始页面可能有它们。因此,它们也没有在表单提交上设置。

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

    1. 使用@ViewScoped bean 在初始访问时保存这些参数的值;
    2. 向您的表单添加一些隐藏的输入字段,以便在表单提交时也发送它们或将&lt;f:param&gt; 嵌套在您的&lt;h:commandButton&gt; 中;
    3. 使用 OmniFaces 的 &lt;o:form includeViewParams="true"&gt; 标签 (Tag documentation / Showcase example) 而不是 &lt;h:form&gt;,因为它会提交到当前 URL 并附加视图参数,前提是它们是使用 &lt;f:viewParam&gt; 设置的(请参阅,例如,BalusC 对Retain original GET request parameters across postbacks 的回复了解详情)。

    【讨论】:

      【解决方案2】:

      我就是这样做的,我只是不能告诉你这是否是最好的方法......

      客户端:

      <h:outputLink target="_blank" value="detalhepesquisa.jsf">
              <h:outputText value="#{te.empresa.nome}" />
              <f:param name="id" value="#{te.empresa.id}"></f:param>
      </h:outputLink>
      

      在你的 bean 中:

      String parametroID = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
      

      【讨论】:

        猜你喜欢
        • 2019-11-29
        • 1970-01-01
        • 2017-04-30
        • 1970-01-01
        • 2013-09-20
        • 2012-04-12
        • 1970-01-01
        • 2017-08-23
        • 2014-12-30
        相关资源
        最近更新 更多