views:

94

answers:

1

This seems like it should be easy to do, but I just can make it work. I'm hooked on the convention plugin in Struts 2.1. However, I need to define some package-level configuration such as a new interceptor stack and exception mappings. I'd like to use the struts.xml file for this, but I can't get the convention-based packages matched to the struts.xml packages. My struts.xml looks like:

<struts>
<constant name="struts.convention.default.parent.package" value="default"/>  
<package name="default" extends="struts-default">
</package>
<package name="root" namespace="/" extends="struts-default">
    <action name="index">
        <result>/index.jsp</result>
    </action>
</package>

<package name="my.package.actions.myaccount" namespace="/myaccount" extends="struts-default">
<interceptors>
    <interceptor name="authenticationInterceptor" class="my.package.interceptors.AuthenticationInterceptor"/>
    <interceptor-stack name="secureStack">
        <interceptor-ref name="authenticationInterceptor"/>
        <interceptor-ref name="defaultStack"/>
    </interceptor-stack>
</interceptors>

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

I have my interceptor in:
/src/my/package/interceptors
and my actions in:
/src/my/package/actions/myaccount

+1  A: 

I figured it out. I changed the name of the package above to just read "myaccount"

Then you can either add this to an individual action by annotation:

@ParentPackage(value = "myaccount")

Or to all actions in the package by adding a package-info.java file to the appropriate directory which includes the following:

@org.apache.struts2.convention.annotation.ParentPackage(value = "myaccount") 

package com.mysite.actions.myaccount;

Hope this saves someone else some time!

David Alt