带有注释错误的Struts 2:无法找到父包

时间:2014-01-31 06:28:23

标签: java struts2 annotations struts2-convention-plugin struts2-tiles-plugin

我正在尝试使用磁贴和Struts2。我在struts2动作类上使用注释,如下所示。我已经使用struts2约定插件定义了一个默认的父包,并且还定义了从struts-default和tiles-default包扩展的包“rcg.console”。但是,当我启动我的tomcat服务器时,我得到以下异常。但是,如果我将操作类的@ParentPackage(value="rcg.console")注释更改为@ParentPackage(value="tiles-default"),则错误消失。如果我没有定义@ParentPackage注释,struts就无法检测到我的“tiles”结果类型。有人可以帮我这个吗?

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;
}

}

1 个答案:

答案 0 :(得分:1)

要将@ParentPackage注释与动作类或Java包一起使用,您必须将有效 Struts包名称指定为值属性。如果Struts无法找到指定的包名称,它将通知您例外的情况

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

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

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

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

<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> 

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