使用带有约定插件的struts.xml [英] Using struts.xml with convention plugin
问题描述
这看起来应该很容易,但我可以让它发挥作用。我迷上了Struts 2.1中的约定插件。但是,我需要定义一些包级配置,例如新的拦截器堆栈和异常映射。我想为此使用struts.xml文件,但我无法获得与struts.xml包匹配的基于约定的包。我的struts.xml看起来像:
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>
我的拦截器在:
/ src / my / package / interceptors
和我的行动:
/ src / my / package / actions / myaccount
I have my interceptor in:
/src/my/package/interceptors
and my actions in:
/src/my/package/actions/myaccount
推荐答案
我明白了。我将上面的包的名称更改为只读myaccount
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")
或者通过将package-info.java文件添加到包含以下内容的相应目录中来执行包中的所有操作:
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!
这篇关于使用带有约定插件的struts.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!