【问题标题】:JSP parameter names in HttpServletRequestHttpServletRequest 中的 JSP 参数名称
【发布时间】:2011-03-23 21:19:38
【问题描述】:

背景

使用 Eclipse Helios、Apache Tomcat、JSP 和 JBoss RichFaces。

输入参数编码如下:

<h:inputHidden name="system_REPORT_RESOURCE" value="city" />
<h:inputHidden name="system_REPORT_FILENAME" value="city" />
<h:inputHidden name="report_REPORT_TITLE" value="City Listing" />
... and others ...

问题

以下代码显示了键的值:

@SuppressWarnings( "unchecked" )
protected void setInputParameters() {
  HttpServletRequest request = getServletRequest();
  Enumeration<String> keys = request.getParameterNames();

  while( keys.hasMoreElements() ) {
    String key = keys.nextElement();

    for( String value : request.getParameterValues( key ) ) {
      System.out.println( "KEY: " + key + " VALUE: " + value );
    }
  }
}

框架修改键名:

KEY: j_id2:report_city VALUE: ab
KEY: j_id2:system_REPORT_RESOURCE VALUE: city
KEY: j_id2:j_id11 VALUE: Report
KEY: j_id2:report_REPORT_TITLE VALUE: City Listing
KEY: j_id2:report_int_max_longitude VALUE: 
KEY: j_id2:report_int_min_latitude VALUE: 
KEY: javax.faces.ViewState VALUE: j_id28
KEY: j_id2:system_REPORT_FILENAME VALUE: city
KEY: j_id2 VALUE: j_id2

更新

&lt;h:form&gt; 标记更改为包含id="form" 属性会导致:

KEY: form:system_REPORT_FILENAME VALUE: city
KEY: form VALUE: form
KEY: form:report_city VALUE: ab
KEY: form:report_int_max_longitude VALUE: 
KEY: form:report_REPORT_TITLE VALUE: Canadian City List
KEY: form:system_REPORT_RESOURCE VALUE: city
KEY: form:report_int_min_latitude VALUE: 
KEY: form:j_id10 VALUE: Report
KEY: javax.faces.ViewState VALUE: j_id1

这更好,但仍然不理想。

更新 2

代码需要一般地解析输入表单参数名称。到目前为止,以下代码可以删除前缀(但感觉很hackish):

  /**
   * Appends the list of HTTP request parameters to the internal parameter
   * map of user input values. Some frameworks prepend the name of the HTML
   * FORM before the name of the input. This method detects the colon and
   * removes the FORM NAME, if present. This means that input parameter
   * names assigned by developers should not contain a colon in the name.
   * (Technically, it is possible, but to avoid potential confusion it
   * should be avoided.)
   */
  protected void setInputParameters() {
    HttpServletRequest request = getServletRequest();
    Iterator<String> keys = getExternalContext().getRequestParameterNames();
    Parameters p = getParameters();

    while( keys.hasNext() ) {
      String key = keys.next();

      for( String value : request.getParameterValues( key ) ) {
        int i = key.indexOf( ':' );

        if( i >= 0 ) {
          key = key.substring( i + 1 );
        }

        p.put( key, value );
      }
    }
  }

问题

什么 API 调用返回不带 j_id2 前缀的参数名称?

删除 j_id2 前缀(和可选的完整冒号)可能会引入依赖于框架的代码。

谢谢!

【问题讨论】:

    标签: jsp tomcat jboss richfaces servlets


    【解决方案1】:

    我认为没有 API 可以做到这一点,j_id2:XXX 是实际的请求参数名称。

    【讨论】:

      【解决方案2】:

      您可以使用标准的 HTML &lt;input&gt; 标签。它工作得很好。事实上,我刚刚发布了我自己的问题的答案,我发现使用它是最好的主意。在这里查看:Passing parameters to PrimeFaces Star Rating component?

      【讨论】:

      • 我看不出在这种情况下会出现什么问题。在我的示例中,您的表单将仅具有一个通用的隐藏输入字段,可通过迭代参数获得。
      • 该框架适用于其他有自己问题的开发人员,这些问题不一定受到&lt;input ...&gt;的限制。例如,他们可能需要 &lt;rich:input 值和其他值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 2016-03-27
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      相关资源
      最近更新 更多