【问题标题】:java.lang.IllegalStateException: Root context attribute is not of type WebApplicationContextjava.lang.IllegalStateException:根上下文属性不是 WebApplicationContext 类型
【发布时间】:2011-06-23 11:17:34
【问题描述】:

我在 Tomcat 6 上的 Liferay 5.2.3 上部署 Portlet。我仅在其中一个 Portlet 上收到此错误。

 java.lang.IllegalStateException: Root context attribute is not of type WebApplicationContext

我做了一些研究,发现 Spring 在需要 Web 应用程序上下文时会实例化一个 portlet 应用程序上下文。但在我的 web.xml 中,我只定义了 contextLoaderListner

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

最重要的是,如果 Spring 正在查找不同的 *.jar 文件,那么除了一个之外,为什么还要部署我的其他 portlet?

经过几次重新部署后,我解决了这个问题。有人可以点亮吗?

【问题讨论】:

    标签: model-view-controller spring listener portlet


    【解决方案1】:

    根本原因似乎是门户/应用程序服务器中的一个静态变量“挂在”portlet 中的一个类的实例上。两个常见的罪魁祸首是 log4j 和 java 日志记录,它们都是应用程序容器常用的。

    有关记录器的更多讨论,请参阅 log4j and the thread context classloaderhttp://logback.qos.ch/manual/loggingSeparation.html。建议将 SLF4J 与 logback 一起使用,或者确保将 log4j.jar 放入您的 WAR 文件中,以便它位于正确的类加载器中(尽管某些容器会阻碍此解决方案)。

    此外,容器中存在的其他一些类可能是原因。日志记录只是一个常见问题。

    【讨论】:

      【解决方案2】:

      听起来您没有定义 contextConfigLocation?在 web.xml 中,除了 contextLoaderListener 之外,您还应该有类似的内容:

      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
              /WEB-INF/applicationContext.xml     
          </param-value>
      
      </context-param>
      
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      

      其中 applicationContext.xml 是 webapp 的普通配置文件。

      如果使用 spring portlet MVC,您的 web.xml 中也应该有这个:

      <servlet>
          <servlet-name>ViewRendererServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>ViewRendererServlet</servlet-name>
          <url-pattern>/WEB-INF/servlet/view</url-pattern>
      </servlet-mapping>
      

      在你的 portlet.xml 中,我猜你有这样的东西来指定你的 portlet:

      <portlet>
      <portlet-name>sample</portlet-name>
      <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
      <supports>
          <mime-type>text/html</mime-type>
          <portlet-mode>view</portlet-mode>
      </supports>
      <portlet-info>
          <title>Sample Portlet</title>
      </portlet-info>
      </portlet>
      

      如果您还没有,请参阅 spring portlet mvc 参考 documentation

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2019-08-25
        • 2018-08-02
        • 1970-01-01
        • 1970-01-01
        • 2020-04-26
        • 2018-03-11
        • 2022-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多