Using struts.xml with convention plugin

☆樱花仙子☆ 提交于 2019-12-11 05:01:55

问题


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:


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!



来源:https://stackoverflow.com/questions/3029376/using-struts-xml-with-convention-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!