【问题标题】:Sitemesh, Cannot construct Factory : com.opensymphony.module.sitemesh.factory.DefaultFactory:Sitemesh,无法构建工厂:com.opensymphony.module.sitemesh.factory.DefaultFactory:
【发布时间】:2015-10-01 11:09:22
【问题描述】:

我用的是2.4.2版的SiteMesh with Spring

<dependency>
        <groupId>opensymphony</groupId>
        <artifactId>sitemesh</artifactId>
        <version>2.4.2</version>
    </dependency>

我使用名称 myApp.war 部署我的应用程序,并且一切正常。我需要部署应用程序 myapp##versionApp.war 名称,而这个名称会出现问题。

错误是

无法构造工厂:com.opensymphony.module.sitemesh.factory.DefaultFactory:com.opensymphony.module.sitemesh.factory.FactoryException:无法读取配置文件:/WEB-INF/sitemesh.xml:java.io。 FileNotFoundException:

我发现WEB-INF/目录/文件中存在sitemesh.xml。

你能帮帮我吗?

【问题讨论】:

    标签: java html spring jsp sitemesh


    【解决方案1】:

    我们在 2.4 版本中也有同样的功能

    DefaultFactory 会尝试使用 ServletContext().getRealPath(),但最终会尝试从 contexPath 中读取。我认为这是一个错误。

    要解决这个问题,您必须创建自己的工厂并将其设置在 web.xml 环境中

    复制代码的 DefaultFactory

    public class MyFactory extends com.opensymphony.module.sitemesh.factory.BaseFactory {
    
        public MyFactory () {
            // Do stuff
            // delete every thing that sets the config path or simply set
    
            String configFile = null;
    
            // Do stuff
        }
        // Do other stuff
    }
    

    您的 web.xml 将如下所示

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        version="2.4">
    
            <env-entry>
                <env-entry-name>sitemesh.factory</env-entry-name>
                <env-entry-type>java.lang.String</env-entry-type>
                <env-entry-value>my.sitemesh.MyFactory</env-entry-value>
            </env-entry>
    

    【讨论】:

      【解决方案2】:

      在为我们的旧应用程序升级 Tomcat 版本从 7 到 9 时,我在 2.3 版中遇到了同样的问题。

      问题是 DefaultFactory 类的这两行。

       this.configFileName = config.getServletContext().getInitParameter("sitemesh.configfile");
       .
       .
       .
       .
      
       is = this.configFile.toURL().openStream();
      

      通过将 web.xml 文件上的 configFileName 定义为 context-param,可以解决文件名问题。

      <context-param>
          <param-name>sitemesh.configfile</param-name>
          <param-value>/WEB-INF/sitemesh.xml</param-value>
      </context-param>
      

      配置文件流上不推荐使用的方法会导致问题。为了解决这个问题,应该编写自定义工厂类。可以直接复制defaultFactory,替换这一行

       is = this.configFile.toURL().openStream();
      

      有了这个

       is = new FileInputStream(this.configFile);
      

      此更改将解决输入流问题。

      我的添加环境条目,您可以将自定义类添加到站点网格。此行应放在 web.xml 文件中

      <env-entry>
          <env-entry-name>sitemesh.factory</env-entry-name>
          <env-entry-type>java.lang.String</env-entry-type>
          <env-entry-value>your.custom.factory.SitemeshFactory</env-entry-value>
      </env-entry>
      

      【讨论】:

        【解决方案3】:

        DefaultFactory.java 使用 Deprecate File.toURL() API。读取 sitemesh.xml 文件时出现问题。

        请将您的站点网格版本更新为sitemesh:2.5-atlassian-11

        sitemesh:2.5-atlassian-11 Bugfix commit

        【讨论】:

          【解决方案4】:

          更改 WEB-INF 中的上下文参数、参数值对我有用。 我知道这不是一个完美的解决方案,但我已经尝试了很多东西,但没有任何效果。

          <context-param>
              <param-name>sitemesh.configfile</param-name>
              <param-value>/opt/tomcat/xxx/yyy/zzz/WEB-INF/sitemesh.xml</param-value>
          

          *xxx/yyy/zzz是我的三级上下文,war文件是这样的xxx#yyy#zzz#app-name.war

          【讨论】:

            猜你喜欢
            • 2014-05-20
            • 2021-10-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-18
            相关资源
            最近更新 更多