Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-2848

Exception while using @Result annotation with result type which doesn't define a default parameter

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • 2.0.12, 2.1.2
    • 2.1.8
    • Plugin - CodeBehind
    • None
    • Patch

    Description

      While using an Result annotation on an action class with the result type of
      JSONResult.class (from the struts2 jsonplugin) like the following code snippet
      suggests:

      @ParentPackage("default")
      @Result(name=Action.SUCCESS,
      value="",type=JSONResult.class,
      params=

      {"noCache", "true", "root", "result"}

      )
      public class SomeAction implements Action

      I came across the following exception:

      Caught OgnlException while setting property 'location' on type 'com.googlecode.jsonplugin.JSONResult'.
      com.opensymphony.xwork2.DefaultActionInvocation.createResult(DefaultActionInvocation.java:205)
      com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:349)
      com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:259)
      ...

      After digging into the code from ClasspathPackageProvider I found the method
      createResultConfig with the following code:

      String defaultParam;
      try

      { defaultParam = (String) resultClass.getField("DEFAULT_PARAM").get(null); }

      catch (Exception e)

      { // not sure why this happened, but let's just use a sensible choice defaultParam = "location"; }

      HashMap params = new HashMap();
      if (configParams != null)

      { params.putAll(configParams); }

      params.put(defaultParam, location);
      return new ResultConfig.Builder((String) key, resultClass.getName()).addParams(params).build();

      Because JSONResult does not define a field DEFAULT_PARAM, an entry called
      "location" is inserted into the params HashMap with the empty string from
      the value element of the Result annotation. This causes the exception later
      because JSONResult does not define a method "setLocation".

      I suspect it is not correct to try to set a property on a result class
      which doesn't define the DEFAULT_PARAM field. I'll attach a patch to this
      issue which fixed the bug for me. The patch was created against the latest
      trunk version of ClasspathPackageProvider. If the patch finds its way into
      the repository the same changes would have to be made in
      ClasspathConfigurationProvider in the 2.0.* tree.

      Attachments

        1. patch_20081024.diff
          1.0 kB
          Lars Martensen

        Activity

          People

            musachy Musachy Barroso
            martensen Lars Martensen
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: