首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迁移到JSF2.3,MyFaces初始化出现问题

迁移到JSF2.3,MyFaces初始化出现问题
EN

Stack Overflow用户
提问于 2019-12-17 23:50:04
回答 1查看 4.3K关注 0票数 1

我正在升级一个由两个项目组成的应用程序,“公共”和“我的应用程序”。现在,我正在将它升级到JSF2.3 (MyFaces)Primefaces 7,我不知道如何克服初始化中的失败。

我应该注意到还有很多其他的变化。这两个项目都已成功升级为OpenJDK 11Tomcat 9Maven。随着JSF2.3的修改,托管bean不再支持JAVA的CDI,这在JAVA 11中的默认支持时间更长了。因此,我使用了OpenWebBeans 2.0DeltaSpike 1.9.1。尽管有所有这些移动的部分,应用程序的工作一直到了更新JSF的程度,所以我相信问题就在这里。

此时,我有两种可能的配置,都失败了。通过一种配置,我得到了以下错误:

代码语言:javascript
复制
[main] ERROR [Catalina].[localhost].[/myapp] - StandardWrapper.Throwable
No Factories configured for this Application. This happens if the faces-initialization 
does not work at all - make sure that you properly include all configuration settings
necessary for a basic faces application and that all the necessary libs are included. Also 
check the logging output of your web application and your container for any exceptions!
If you did that and find nothing, the mistake might be due to the fact that you use some
special web-containers which do not support registering context-listeners via TLD files 
and a context listener is not setup in your web.xml.
A typical config looks like this;
<listener>
  <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

因此,很明显的解决方案是添加侦听器,但是接下来我得到如下信息:

代码语言:javascript
复制
[main] ERROR myfaces.config.FacesConfigurator - No ManagedBeanDestroyerListener instance
found, thus @PreDestroy methods won't get called in every case. This instance needs to be
published before configuration is started.

这似乎是一个排序问题,在这个问题上,StartupServletContextListener在Faces配置开始之前没有发布ManagedBeanDestroyerListener。然而,在我的研究中,我读到不应该需要StartupServletContextListener,因为它是从JSF中的TLD自动加载的。

有人知道这是怎么回事吗?我怎么能熬过这一关?

下面是我的pom和web.xml的简略版本:

common/pom.xml

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>common</artifactId>
    <version>2.1.8</version>

    <properties>
       <dependency.locations.enabled>false</dependency.locations.enabled>   
       <owb.version>2.0.12</owb.version>
       <deltaspike.version>1.9.1</deltaspike.version>      
    </properties>

    <repositories>
        <repository>
            <id>local_repository</id>
            <url>https://server.company.com/repository</url>
        </repository>
    </repositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.deltaspike.distribution</groupId>
                <artifactId>distributions-bom</artifactId>
                <version>${deltaspike.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-api</artifactId>
            <version>2.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-impl</artifactId>
            <version>2.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.myfaces.core.internal</groupId>
            <artifactId>myfaces-impl-shared-public</artifactId>
            <version>2.3.5</version>
        </dependency>

        <!-- Stored in local maven repository -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>7.0</version>
        </dependency>

        <!-- Stored in local maven repository -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>4.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>2.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
            <version>1.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>catalina</artifactId>
            <version>6.0.53</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-util</artifactId>
            <version>9.0.12</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-bundle</artifactId>
            <version>2.3.5</version>
        </dependency>


        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0.SP1</version>
        </dependency>

        <!-- OpenWebBeans - implements CDI Container -->
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-spi</artifactId>
            <version>${owb.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-impl</artifactId>
            <version>${owb.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-web</artifactId>
            <version>${owb.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-jsf</artifactId>
            <version>${owb.version}</version>
        </dependency>

        <!-- DeltaSpike - manages CDI container -->
        <dependency>
            <groupId>org.apache.deltaspike.core</groupId>
            <artifactId>deltaspike-core-api</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-jsf-module-api</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.deltaspike.core</groupId>
            <artifactId>deltaspike-core-impl</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-jsf-module-impl</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--  CDI Control API -->
        <dependency>
            <groupId>org.apache.deltaspike.cdictrl</groupId>
            <artifactId>deltaspike-cdictrl-api</artifactId>
            <scope>compile</scope>
        </dependency>       

        <!--  CDI Control for OpenWebBeans -->
        <dependency>
            <groupId>org.apache.deltaspike.cdictrl</groupId>
            <artifactId>deltaspike-cdictrl-owb</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-servlet-module-api</artifactId>
            <version>${deltaspike.version}</version>
            <scope>compile</scope>
        </dependency>       
        <dependency>
            <groupId>org.apache.deltaspike.modules</groupId>
            <artifactId>deltaspike-servlet-module-impl</artifactId>
            <version>${deltaspike.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>test</testSourceDirectory>
        <resources>
            ...
        </resources>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <debug>${debugBuild}</debug>
                    <debuglevel>lines,vars,source</debuglevel> 
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                ...
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                ...
            </plugin>

        </plugins>
    </build>    
</project>

myapp/pom.xml

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company</groupId>
  <artifactId>myapp</artifactId>
  <version>0.1.0</version>
  <packaging>war</packaging>

  <properties>
       <commonVersion>2.1.8</commonVersion>
       <dependency.locations.enabled>false</dependency.locations.enabled>
  </properties>

  <dependencies>
    <dependency>
        <groupId>com.company</groupId>
        <artifactId>common</artifactId>
        <version>${commonVersion}</version>
    </dependency>
  </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>test</testSourceDirectory>
    <resources>
      <resource>
        <directory>resources</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>11</source>
          <target>11</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

common/META-INF/web-fragment.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-fragment metadata-complete="true" version="3.0"
              xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">

    <name>common</name>

    <filter>
        <filter-name>ResourceFilter</filter-name>
        <filter-class>com.company.common.web.ResourceFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
        <filter-name>ResourceFilter</filter-name>
        <url-pattern>/javax.faces.resource/*</url-pattern>
        <url-pattern>/resources/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>100000000</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>


    <listener>
        <display-name>httpSessionListener</display-name>
        <listener-class>com.company.common.usersession.UserSessionListener</listener-class>
    </listener>

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

    <context-param>
      <param-name>org.apache.myfaces.annotation.USE_CDI_FOR_ANNOTATION_SCANNING</param-name>
      <param-value>true</param-value>
    </context-param>   
    <context-param>
        <param-name>javax.faces.FACELETS_BUFFER_SIZE</param-name>
        <param-value>65535</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>mytheme</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION</param-name>
        <param-value>20</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION</param-name>
        <param-value>2</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES</param-name>
        <param-value>31536000000</param-value> <!-- 1 year -->
    </context-param>
    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>
</web-fragment>

myapp/WEB-INF/web.xml

代码语言:javascript
复制
<?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"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   version="3.0">

  <absolute-ordering>
    <name>common</name> <!-- Get the common web fragment -->
  </absolute-ordering>

    <!-- 
        AppApplicationContextListenerhas to happen before WebBeansConfigurationListener
        so that the application context (e.g. app name, db connections) is set before
        web beans are scanned  
    -->
    <listener>
        <listener-class>com.company.application.app.AppApplicationContextListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
    </listener>

<!-- INCLUDE ??? -->
    <listener>
      <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
    </listener>

  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>



  <!-- Faces Servlet can't be moved into common's web-fragment.xml due to a bug in Apache's MyFaces -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
<!-- or possibly:                         -->
<!--     <url-pattern>*.jsf</url-pattern> -->
  </servlet-mapping>

</web-app>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-21 00:21:41

[更新3-1-2020年:我正在更新这个条目以删除一些错误的建议。具体来说,将faces-config.xml重命名为standard-faces-config.xml使MyFaces无法正确初始化。]

我已经解决了大部分问题。有几个组件干扰了我的项目。

Maven:

对于使用Eclipse的Maven来说,这是一个痛苦的教训,我必须确保重新运行Maven构建并清理部署文件夹,以便实际执行更改。在那之后,这里是我发现和修正的要点。

显然,

  • 在处理第二个引用的项目时,m2e没有正确地处理测试jars。所以使用<classifier>tests</classifier><type>test-jar</type><classifier>tests</classifier>块不能工作,但是下面的代码将把测试源复制到依赖的项目中进行编译。(确保适当地编辑<source>值,并在m2e中运行的Maven中启用m2e配置文件)

代码语言:javascript
复制
    <profiles>
      <profile>
        <id>m2e</id>
        <activation>
          <property>
            <name>m2e.version</name>
          </property>
        </activation>
        <build>
          <plugins>
            <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>build-helper-maven-plugin</artifactId>
              <version>3.0.0</version>
              <executions>
                <execution>
                  <id>include-test-source-eclipse</id>
                  <phase>generate-test-sources</phase>
                  <goals>
                    <goal>add-test-source</goal>
                  </goals>
                  <configuration>
                    <sources>
                      <source>../common/test/java</source>
                    </sources>
                  </configuration>
                </execution>
              </executions>
            </plugin>
          </plugins>
        </build>
      </profile>
    </profiles>

  • org.apache.myfaces.core:myfaces-bundle只是将org.apache.myfaces.core:myfaces-apiorg.apache.myfaces.core:myfaces-impl合并到一个包中。包括任何一种/或两者都包括在内。(我收到的建议是使用单独的包,而不是我在Apache上看到的bundle.)
  • Despite,DeltaSpike并不能使启动CDI (OpenWebBeans)变得更容易,所以我删除了它。注册OpenWebBeans启动监听器似乎就足够了(见下文),cobertura (org.codehaus.mojo:cobertura-maven-plugin)不支持
  • JDK 11。人们推荐JaCoCo #67代替它。

web.xml

在从托管bean切换到CDI时,web.xml中需要使用以下行。托管bean,虽然不推荐使用,但仍然默认为启用。

代码语言:javascript
复制
<context-param>
    <param-name>org.apache.myfaces.SUPPORT_MANAGED_BEANS</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>org.apache.myfaces.annotation.USE_CDI_FOR_ANNOTATION_SCANNING</param-name>
    <param-value>true</param-value>
</context-param> 

下面是我目前使用的侦听器(以及它们的顺序):

代码语言:javascript
复制
<listener>
    <listener-class>com.company.application.app.MyApplicationContextListener</listener-class>
</listener>

<listener>
    <listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
</listener>

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

<listener>
  <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

faces-config.xml:

别做我做的事。不要创建standard-faces-config.xml文件。这会阻止面正确地初始化。[没有找到普通项目的faces-config.xml文件,重新命名它似乎解决了问题。坏主意。]这个错误导致MyFaces无法创建<action-listener>org.primefaces.application.DialogActionListener</action-listener>,也导致了this

总之..。

  • 似乎确实有必要在web.xml中包括MyFaces StartupServletContextListener。
  • 奇怪的“未发布Bean破坏者”错误似乎是由于切换到CDI,而不是禁用MyFaces的托管Bean支持。由于我的initialization.

文件阻塞了正确的,

  • 多个启动问题

@tandraschko:非常感谢您的帮助和关注,因为我在这方面苦苦挣扎!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59383725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档