Posts tagged ‘java’

Struts 2 ParametersInterceptor

Saturday, April 5th, 2008

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.

Struts 2 bug – s:submit tag type=button rendering

Wednesday, November 28th, 2007

In Struts 2.0.11, the s:submit tag with type=button does not render properly.

While working on a project using Struts 2, I was attempting to create a HTML button that looked something like:

<button type="button" class="button positive save">
    <img src="/images/tick.png" alt="Save"> Save
</button>

I followed the Struts 2 Tag Reference for the submit tag and tried the following:

<s:submit type="button" cssClass="button positive save">
    <img src="/images/tick.png" alt="Save"> Save
</s:submit>

Instead of rendering like my example above, it looks like:

<img src="/images/tick.png" alt="Save"> Save
<button type="submit" id="test_0" value="Submit" class="button positive save">Submit</button>

Not exactly what I had in mind… ;)

Read the rest of this entry »