1

我正在尝试将磁贴与 Struts 2 一起使用。我在操作类上使用注释,如下所示。我已经使用 Convention Plugin 定义了一个默认父包,并且还定义了从包rcg.console扩展的struts-defaulttiles-default

但是,当我启动 Tomcat 服务器时,出现以下异常。但是,如果我将 @ParentPackage(value="rcg.console") 操作类上的注释更改为 @ParentPackage(value="tiles-default"),错误就会消失。如果我没有定义@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;
}

}
4

1 回答 1

1

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

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

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

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

但是,您的包没有创建,因为您没有struts.xml向 Web 应用程序提供配置文件,或者调度程序无法访问它。默认情况下struts.xml放置在类路径中,它可以由上下文类加载器加载,或者您可以使用configStruts 过滤器的 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> 

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

于 2014-01-31T10:44:30.507 回答