HttpServletRequest 中的 JSP 参数名称

发布于 2024-10-26 02:24:46 字数 2911 浏览 3 评论 0原文

背景

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

更新

更改 标记以包含 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

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

  /**
   * 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 前缀(和可选的完整冒号)可能会引入依赖于框架的代码。

谢谢你!

Background

Using Eclipse Helios, Apache Tomcat, JSPs, and JBoss RichFaces.

Input parameters are coded as follows:

<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 ...

Problem

The following code shows the keys' values:

@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 );
    }
  }
}

The framework modifies the keys' names:

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

Update

Changing the <h:form> tag to include an id="form" attribute results in:

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

This is better, but still not ideal.

Update 2

The code needs to parse input form parameter names generically. So far, the following code works to remove the prefix (but it feels 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 );
      }
    }
  }

Question

What API call returns the parameter names without the j_id2 prefix?

Removing the j_id2 prefix (and optional full colon) would likely introduce framework-dependent code.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

层林尽染 2024-11-02 02:24:46

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

I don't think there is API to do that, j_id2:XXX is the actual request parameter name.

七堇年 2024-11-02 02:24:46

您可以使用标准 HTML 标记。它工作得很好。事实上,我刚刚发布了我自己问题的答案,我发现使用它是最好的主意。请参阅此处:将参数传递给 PrimeFaces 星级评定组件?

You could use a standard HTML <input> tag. It works just fine. In fact, I just posted an answer to my own question where I found using it was the best idea. See it here: Passing parameters to PrimeFaces Star Rating component?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文