首先在struts.xml中设置以下常量设置:
设置Convention插件是否从jar包中搜索Action类 [可选]
默认值为true
<constant name="struts.convention.action.disableJarScanning" value="true"
/>
设置Convention插件文件协议类型
<constant name="struts.convention.action.fileProtocols" value="jar,wsjar"
/>
设置Convention插件需要搜索的jar包
<constant name="struts.convention.action.includeJars" value=".*?/struts2-action*.*?jar(!/)?"
/>
该常量指定包作为根包来搜索Action类。
Convention插件除了扫描默认的action,actions,struts,struts2,还会扫描该常量指定的一个或多个包,
Convention会试图从指定包中发现Action类。
<constant name="struts.convention.action.packages" value="com.home.prog"
/>
设置完常量后,还要保证打出来包要符合搜索条件,之前我就是问题出在了这里,由于我是用的maven构建的项目,于是在pom.xml配置文件中加入了以下打包参数:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
运用maven打包出来的jar中包含了 META-INF 目录
然后启动程序,顺利找到jar包中的Action
为了看到struts2应用里的Action等各种资源的影射情况,struts2提供了Config
Browser插件。
使用方法,将struts2-config-browser-plugin插件引入到项目中。
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
打开首页地址:http://localhost:端口/发布应用名称/config-browser/actionNames.action这里可以看到Config Browser插件的首页。
注:在编辑器调试布置程序中打出来的包中并不包含 META-INF 目录,struts2不能搜索到jar中的Action。