HttpServletRequest中的JSP参数名称

时间:2011-03-23 21:19:39

标签: jsp tomcat jboss richfaces servlets

背景

使用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

更新

更改<h:form>代码以包含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前缀(和可选的完整冒号)可能会引入依赖于框架的代码。

谢谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用标准的HTML <input>标记。它工作得很好。事实上,我刚刚发布了一个我自己的问题的答案,我发现使用它是最好的主意。请在此处查看:Passing parameters to PrimeFaces Star Rating component?