Struts 2 ParametersInterceptor

Are your logs being filled up with errors like:

ERROR - ParametersInterceptor.setParameters(204) | ParametersInterceptor - [setParameters]: Unexpected Exception caught setting '_' on 'class com.company.web.MyAction: Error setting expression '_' with value '[Ljava.lang.String;@1491ddc'

If so, it’s because Struts 2 is parsing the query string/post data and trying to “set” a value for each parameter it finds.

I’m working on an Struts 2 application utilizing the jQuery JavaScript library for the UI and the Displaytag tag library for displaying tables.

With jQuery, I’m using the “no-cache” option (cache: false) on all Ajax calls, which adds “_=timestamp” to each request. Since I don’t have a property called “_” in my Action class, I get an error (mentioned above) in my logs for each request.

Same with Displaytag, except the parameters causing errors are in the form of “d-#-X“, where “#” is a unique id (usually 4 or 5 digits) and “X” is either p, s or o. They are used to determine the page (p) and/or the table sort order (o for asc/desc and s for which column).

The solution: configure Struts (struts.xml) to ignore these parameters:

<interceptors>
   <interceptor-stack name="defaultStack">
      <interceptor-ref name="params">
         <!-- 
            Excludes the jQuery no-cache _ parameter and
            the Displaytag d-#-X parameter(s)
         -->
         <param name="excludeParams">
            _,d-\d+?-[sop]
         </param>
      </interceptor-ref>
   </interceptor-stack>
</interceptors>

If you have a different parameters that are causing problems, just put them in the excludeParams node, comma separating multiple parameters.

5 thoughts on “Struts 2 ParametersInterceptor”

  1. prototype.js (v1602) appends an additional request parameter (‘&_=’) for certain browsers. To suppress via code, implement ParameterAware like so:

    @SuppressWarnings("unchecked")
    @Override
    public void setParameters(Map parameters) {
      // prototype.js appends a request parameter '_=' 
      if (parameters.get("_") != null) {
        parameters.remove("_");
      }
    }
  2. @s2-newB – Thanks for the suggestion. Since that is the same parameter that jQuery uses, wouldn’t the excludeParams example that I gave do the same thing?

Comments are closed.

Scroll to Top