0

我正在创建结合了 Struts 2 和 Spring 3 框架的应用程序。

下面是我的 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>Struts 2</display-name>
    <welcome-file-list>
        <welcome-file>jsp/index.jsp</welcome-file>
    </welcome-file-list>
    <!-- Filter Start -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>
    <!-- Filter End -->



    <!-- Filter Mapping Start -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <!-- Filter Mapping End -->

    <!-- Listener Start -->
    <!-- Struts Start -->
    <listener>
        <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
    </listener>
    <!-- Struts End -->

    <!-- Spring Start -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring End -->
    <!-- Listener End -->

    <!-- Servlet Start -->
    <servlet>
        <servlet-name>sitemesh-freemarker</servlet-name>
        <servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class>
        <init-param>
            <param-name>default_encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Servlet End -->

    <!-- Servlet Mapping Start -->
    <servlet-mapping>
        <servlet-name>sitemesh-freemarker</servlet-name>
        <url-pattern>*.ftl</url-pattern>
    </servlet-mapping>
    <!-- Servlet Mapping End -->
</web-app>

我的 applicationContext.xml 如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="test1" class="tc.account.LoginAction"/>


</beans>

而struts.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources"
        value="ApplicationResources" />
   <package name="default" extends="struts-default">
      <action name="index">
            <result >jsp/index.jsp</result>
      </action>   
<!--       <action name=""> -->
<!--             <result>jsp/index.jsp</result> -->
<!--       </action>    -->
      <action name="login"
            class="tc.account.LoginAction">
            <result name="success">jsp/index.jsp</result>
            <result name="error">jsp/account/login.jsp</result>
        </action> 
   </package>
</struts>

当我删除 Spring 侦听器时,应用程序工作正常。但我想包括 IOC 和 DI 的 Spring 框架。我已经配置了相同的内容,web.xml但是在加载应用程序时它给了我以下错误:

Sep 30, 2013 11:19:16 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.apache.struts2.dispatcher.ng.listener.StrutsListener
Class: com.opensymphony.xwork2.spring.SpringObjectFactory
File: SpringObjectFactory.java
Method: getClassInstance
Line: 230 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:230:-1
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:502)
    at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
    at org.apache.struts2.dispatcher.ng.listener.StrutsListener.contextInitialized(StrutsListener.java:46)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    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$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at com.opensymphony.xwork2.spring.SpringObjectFactory.getClassInstance(SpringObjectFactory.java:230)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyResultType(XmlConfigurationProvider.java:604)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addResultTypes(XmlConfigurationProvider.java:574)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:530)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:292)
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:250)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:446)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:490)

我没有看到我包含了所有必需的库的问题。

4

1 回答 1

0

最后我解决了这个问题。我更改了 web.xml 文件,首先定义了 StrutsListener 然后是 SpringListener。

    <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">

        <display-name>Struts 2</display-name>
        <welcome-file-list>
            <welcome-file>jsp/index.jsp</welcome-file>
        </welcome-file-list>
        <!-- Filter Start -->
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>

        <filter>
            <filter-name>sitemesh</filter-name>
            <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
        </filter>
        <!-- Filter End -->



        <!-- Filter Mapping Start -->
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/</url-pattern>
        </filter-mapping>

        <filter-mapping>
            <filter-name>sitemesh</filter-name>
            <url-pattern>/</url-pattern>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>FORWARD</dispatcher>
        </filter-mapping>
        <!-- Filter Mapping End -->

        <!-- Listener Start -->

<!-- Spring Start -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- Spring End -->
<!-- Struts Start -->
        <listener>
            <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
        </listener>
        <!-- Struts End -->


        <!-- Listener End -->

        <!-- Servlet Start -->
        <servlet>
            <servlet-name>sitemesh-freemarker</servlet-name>
            <servlet-class>org.apache.struts2.sitemesh.FreemarkerDecoratorServlet</servlet-class>
            <init-param>
                <param-name>default_encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!-- Servlet End -->

        <!-- Servlet Mapping Start -->
        <servlet-mapping>
            <servlet-name>sitemesh-freemarker</servlet-name>
            <url-pattern>*.ftl</url-pattern>
        </servlet-mapping>
        <!-- Servlet Mapping End -->
    </web-app>
于 2013-09-30T07:56:13.193 回答