如何使用替代版本的Mojarra与Glassfish 4(Netbeans/Maven)

Cra*_*ver 13 jsf netbeans glassfish mojarra

我已经使用Eclipse/Ant一段时间了,并决定看看Netbeans/Maven的JSF开发.一路上,我想我会尝试使用比我安装的更新的Mojarra版本glassfish/modules.我似乎无法让它工作,因为我是Maven和Netbeans的新手,我不确定它是否适用于那些或其他东西.

我做了一个非常简单的应用程序,它使用支持bean从索引页面打印出Mojarra版本:

Info.java:

@Named
@RequestScoped
public class Info
{
  public String getVersion()
  {
    return FacesContext.class.getPackage().getImplementationVersion();
  }
}
Run Code Online (Sandbox Code Playgroud)

index.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
  <h:body>
    <h:outputText value="The Mojarra version is #{info.version}" />
  </h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我看到的页面内容

Mojarra版本是2.2.5

正如我所料(我把2.2.5的罐子放glassfish/modules回去了一段时间).然后,我在Mojarra网站上添加了对Mojarra 2.2.7的依赖:

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.faces</artifactId>
  <version>2.2.x</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

用"x"代替"7".生成的POM文件看起来像这样(大部分是来自Netbeans的样板文件):

POM.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.mycompany</groupId>
  <artifactId>mavenproject1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>mavenproject1</name>

  <properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.faces</artifactId>
      <version>2.2.7</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <compilerArguments>
            <endorseddirs>${endorsed.dir}</endorseddirs>
          </compilerArguments>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <outputDirectory>${endorsed.dir}</outputDirectory>
              <silent>true</silent>
              <artifactItems>
                <artifactItem>
                  <groupId>javax</groupId>
                  <artifactId>javaee-endorsed-api</artifactId>
                  <version>7.0</version>
                  <type>jar</type>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,输出没有区别,它仍然显示版本2.2.5.我发现这篇文章的答案来自@BalusC,提到Glassfish必须明确告知覆盖已安装的faces版本以使用与应用程序一起部署的备用版本,方法是将其放入WEB-INF/glassfish-web.xmlGF3的早期版本中:

<class-loader delegate="false" />
<property name="useBundledJsf" value="true" />
Run Code Online (Sandbox Code Playgroud)

虽然我正在使用GF4,但我想我会尝试一下,并添加了一个glassfish-web.xml:

glassfish-web.xml:

<glassfish-web-app error-url="">
  <class-loader delegate="false" />
  <property name="useBundledJsf" value="true" />
</glassfish-web-app>
Run Code Online (Sandbox Code Playgroud)

结果是一个页面显示带有空版本字符串的消息:

Mojarra版本是

GF日志也表示没有错误..war创建的文件包含WEB-INF/lib/javax.faces-2.2.7.jar,所以我怀疑问题与GF配置有关,而不是与Netbeans/Maven包装有关.

我知道我可以下载2.2.7 jar并将其放在glassfish/modules目录中(这就是我开始使用2.2.5的方式).但最终的目标是以特定于应用程序的方式尝试JSF的替代版本(理想情况下,只需更改JSF依赖项的Maven坐标),而不是更改GF安装.无论如何通过配置应用程序而不改变GF安装来做到这一点?


2015年9月14日更新:我建立了一个新的Netbeans项目:

  • GlassFish 4.1(开箱即用),默认情况下有Mojarra 2.2.7.
  • NB Maven/Web应用程序项目,带有JSF 2.2 Framework.
  • 修改了index.xhtml文件,如上图所示.
  • 添加了一个Info.java类,其中包含上面的内容(javax.enterprise.context.RequestScoped,javax.inject.Named).
  • 添加了WEB-INF/glassfish-web.xml并添加了上面显示的行.
  • 如上所示,将依赖项添加到pom.xml文件,但使用的是javax.faces 2.2.12版.

基本上,我得到了相同的结果.如果useBundledJsf属性设置不在glassfish-web.xml文件中,则版本显示为2.2.7(即使war文件中包含2.2.12).如果useBundledJsf属性设置在那里,并且类加载器的更改为"false",则版本为空.

我还通过更改glassfish安装中的javax.faces文件来安装Mojarra 2.2.10以使用2.2.10.当我这样做时,我得到相同的结果 - 版本是空的,或者它显示2.2.10(仍然配置2.2.12,并且在两种情况下都在战争中).

Neb*_*chi 1

另一种解决方案是尝试Payara 4.1.1.154 ,因为它与Mojarra 2.2.12捆绑在一起

我认为这可能是满足您需求的一个很好的解决方案。

This section details the modules that have been updated since the last release (4.1.153).

Mojarra 2.2.12
Webservices 2.3.2-b608
JAXB 2.2.12-b141219.1637
JAXB-API 2.2.13-b141020.1521
Weld 2.2.16.Final
Tyrus 1.11
JBatch 1.0.1-b09
Grizzly 2.3.23
HK2 2.4.0-b32
Jersey 2.22
Hazelcast 3.5.2
Run Code Online (Sandbox Code Playgroud)