Global results with Struts 2 and convention plugin

LordHelmchen picture LordHelmchen · Sep 26, 2013 · Viewed 7.3k times · Source

i would like to have some global results in my application. In good old XML configuration it would look like:

<global-results>
  <result name="error" type="redirectAction">
    <param name="actionName">index</param>
    <param name="namespace">/</param>
  </result>
</global-results>

But as I'm using the convention plugin the global results in the XML seem to be ignored so how could I implement this using the convention plugin? I don't want to have all my action classes extend a custom class that has those global results defined. I think the package-info.java should be my friend but all i can define there having something to do with results is @org.apache.struts2.convention.annotation.ResultPath.

Just to make clear: I don't want to avoid struts.xml configuration - I just want to have some working global forwards so in case of an error in any action i want to forward the user to a central error page. This is currently not working with my configuration. If you see the problem in my struts.xml or my action and can help me to fix it it would be perfectly fine.

Maybe the order in the struts.xml matters? Here's the structure of my struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <constant name="struts.devMode" value="false" />
  <constant name="struts.convention.result.path" value="/content/"/>
  <constant name="struts.convention.default.parent.package" value="my-package"/>
  <constant name="struts.convention.package.locators.disable" value="true"/>
  <constant name="struts.convention.action.packages" value="..."/>
  <constant name="struts.custom.i18n.resources" value="global" />
  <constant name="struts.multipart.maxSize" value="10485760" />
  <package name="my-package" extends="struts-default,json-default" namespace="/">
    <result-types>
      <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
    </result-types>

    <interceptors>
      <interceptor name="login" class="loginInterceptor" />
      <interceptor name="pagetitle" class="pagetitleInterceptor"></interceptor>

      <interceptor-stack name="secureStack">
        ...
      </interceptor-stack>

      <interceptor-stack name="insecureStack">
        ...
      </interceptor-stack>
    </interceptors>

    <default-interceptor-ref name="secureStack" />

    <global-results>
      <result name="error" type="redirectAction">
        <param name="actionName">index</param>
        <param name="namespace">/</param>
      </result>
    </global-results>
  </package>
</struts>

in my action I have:

public class MyActionClass extends ActionSupport {
  @Actions({ @Action(value = "my-action", results = { @Result(name = "success", type = "tiles", location = "my.location") }) })
  public final String myAction() throws Exception {
    return ERROR;
  }
}

of course myAction has more functionality - this is just for illustration. When the action is executed it forwards to the my-action.jsp without using tiles but I expected it to forward to /index.action.

Answer

Roman C picture Roman C · Sep 26, 2013

Unfortunately you can't define the Result or Results annotation on the package using convention plugin. You have to define global results in xml configuration and they aren't ignored because the runtime configuration is defined regardless which configuration provider you use. The workaround is to define Result or Results on the base class.