Struts2带注释错误:无法定位父包



我正在尝试将tiles与Struts2一起使用。我正在操作类上使用注释,如下所示。我使用Convention插件定义了一个默认的父包,还定义了从struts-defaulttiles-default包扩展而来的包rcg.console

但是,当我启动Tomcat服务器时,会出现以下异常。但是,如果我改变@ParentPackage(value="rcg.console")对的操作类的注释CCD_ 5,错误消失了。如果我不定义@ParentPackage注释,struts将无法检测到我的"tiles"result-type。有人能帮我吗?

SEVERE: Exception starting filter struts2
Unable to load configuration. - [unknown location]
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:501)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4656)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5312)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: Unable to load configuration. - [unknown location]
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:445)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:489)
... 14 more
Caused by: Unable to locate parent package [rcg.console] for [class com.tumri.rcg.actions.ConsoleAction] - [unknown location]
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.getPackageConfig(PackageBasedActionConfigBuilder.java:1020)
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildConfiguration(PackageBasedActionConfigBuilder.java:666)
at org.apache.struts2.convention.PackageBasedActionConfigBuilder.buildActionConfigs(PackageBasedActionConfigBuilder.java:348)
at org.apache.struts2.convention.ClasspathPackageProvider.loadPackages(ClasspathPackageProvider.java:53)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:268)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
... 16 more

我的truts.xml文件:

<!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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.convention.default.parent.package" value="rcg.console"/>
<constant name="struts.custom.i18n.resources" value="messages" />
<package name="rcg.console" extends="struts-default, tiles-default" namespace="/"></package>
</struts>

我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>rcg-webapp</display-name>
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>
</web-app>

我的tiles.xml文件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="rcg.defaultLayout"
template="/layouts/default/defaultLayout.jsp">
<put-attribute name="title"     value="RCG Console" />
<put-attribute name="header"    value="/layouts/default/header.jsp" />
<!-- <put-attribute name="tabs"         value="/layouts/default/tabs.jsp" /> -->
<put-attribute name="body"      value="" />
<put-attribute name="footer"    value="/layouts/default/footer.jsp" />
</definition>
<definition name="rcg.consoleHome" extends="rcg.defaultLayout">
<put-attribute name="body" value="/jsp/consoleHome.jsp" />
</definition >
</tiles-definitions>

我的行动类:

package com.tumri.rcg.actions;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@Namespace(value="/")
@ParentPackage(value="rcg.console")
@Results(value={@Result(name="success", type="tiles", location ="rcg.consoleHome")})
@Actions(value={@Action(""), @Action("console")})
public class ConsoleAction extends ActionSupport {
/**
* 
*/
private static final long serialVersionUID = 4622874962298947031L;
Logger logger = Logger.getLogger(ConsoleAction.class);
@Override
public String execute() throws Exception {

System.out.println("Executing Index Action...");
logger.info("Executing Index Action...");
return SUCCESS;
}
}

要将@ParentPackage注释与操作类或Java包一起使用,您必须指定有效的Struts包名称作为值属性。如果Struts找不到指定的包名称,它会通知您一个异常,就像您的一样

Unable to locate parent package [xxx] for [class yyy]

这是正常的,在约定插件配置提供程序根据约定创建自己的配置之前,指定的Struts包必须存在于Struts运行时配置中。

如果您将Struts包名称更改为类似tiles-defaultconvention-default的包,它将起作用,因为这些Struts包是通过基于XML的配置提供程序从插件配置文件中创建的。

但是,您的包没有创建,因为您没有向web应用程序提供配置文件struts.xml,或者调度程序无法访问它。默认情况下,struts.xml被放置在类路径上,上下文类加载器可以在其中加载它,或者您可以使用Struts过滤器的configinit参数指定配置文件的位置。

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>../../location/to/the/struts.xml,struts-default.xml,struts-plugin.xml</param-value>
</init-param>
</filter> 

默认结果类型可用于您的包应该扩展的父包。

相关内容

  • 没有找到相关文章

最新更新