PrimeFaces不渲染 [英] PrimeFaces Not Rendering

查看:102
本文介绍了PrimeFaces不渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的maven项目中,我创建了一个"index.xhtml"文件.当我构建并运行该项目时,Web浏览器未显示任何内容.当我查看源代码时,仍然可以看到源代码,而不是普通的html标签.

我尝试通过以下方式创建XHTML文件:

  1. 选择项目,右键单击"New ..",然后选择"Other ..",然后选择"Web"类别,然后选择"JSF Page".
  2. 选择项目,右键单击"New ..",然后选择"Other ..",然后选择"JavaServer Faces"类别,然后选择"JSF Page".
  3. 选择项目,右键单击新建..",然后选择其他..",然后选择其他"类别,然后选择"XHTML页面".

以上方法均无效.

Maven POM

<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>ProfitV10</groupId>
<artifactId>ProfitV10</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>ProfitV10</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>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>3.3.1</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</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>6.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <p:spinner />
    </h:body>
</html>

这是第一种情况.第二种情况是,我已经有一个可以在PrimeFaces上正常运行的NetBeans Web应用程序项目. "index.html"确实呈现.但是,当我创建一个新的JSF页面并包含PrimeFaces命名空间xmlns:p="http://primefaces.org/ui"时,该特定的JSF和其他新创建的JSF页面将无法呈现.

一定是我想念的东西,但我不知道是什么.

我的环境如下:

  • JDK6
  • Netbeans 7.1.2
  • PrimeFaces 3.3
  • Apache 7.0.2
  • Windows 7(32位)

谢谢.

解决方案

由于您使用/index.xhtml作为欢迎文件,因此您还应该将FacesServlet映射到该扩展名.

如果不需要现有的前缀映射,请替换为:

 <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
 

否则,只需添加*.xhtml模式,就可以得到:

 <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
 

请注意,您几乎总是希望在其中使用*.xhtml映射,以防止公开公开Facelets的源代码.

更新:

.jsf和.xhtml之间的区别:

.jsf是默认的映射之一(如果您在JSF 2.1中根本没有声明FacesServlet,那么您将获得它).它源自磁盘上物理文件具有的扩展名(.jsp或.xhtml)与URL中使用的扩展名之间的差异.

不幸的是,这些在历史上是不同的,因为JSF是一种附加技术,必须通过单独的显式Servlet(Faces Servlet)进行.纯JSP从来没有此限制,因为它是内置的. JSF页面最初仅在JSP中编写,因此,如果您仅在URL请求中使用.jsp,则该容器将直接转到JSP并跳过Faces Servlet.

因此必须为此发明一个解决方法,那就是将Faces Servlet映射到*.jsf,然后Faces Servlet将直接定向到JSP文件,该文件在磁盘上仍具有扩展名.jsp. /p>

我个人认为,简单的*.xhtml*.xhtml映射是唯一可行的现成选项,但是由于向后兼容,JSF似乎无法再将此设置为默认值.

In my maven project, I created an "index.xhtml" file. When I built and ran the project, the web browser didn't show anything. When I view source, I could still see the source code, instead of the normal html tags.

I have tried creating the XHTML file by:

  1. Selecting the project, right-click New.. and select Other.. and select "Web" category and then JSF Page.
  2. Selecting the project, right-click New.. and select Other.. and select "JavaServer Faces" category and then JSF Page.
  3. Selecting the project, right-click New.. and select Other.. and select "Other" category and then XHTML Page.

None of the methods above worked.

Maven POM

<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>ProfitV10</groupId>
<artifactId>ProfitV10</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>ProfitV10</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>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>3.3.1</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</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>6.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <p:spinner />
    </h:body>
</html>

That's the first scenario. The second scenario, I already have a NetBeans Web Application project running ok with PrimeFaces. The "index.html" did render. But when I created a new JSF page and included the PrimeFaces namespace, xmlns:p="http://primefaces.org/ui", But this particular JSF and other newly created JSF page won't render.

Must be something which I am missing, but I do not know what.

My environment is as below:

  • JDK6
  • Netbeans 7.1.2
  • PrimeFaces 3.3
  • Apache 7.0.2
  • Windows 7 (32-bit)

Thanks.

解决方案

Since you're using /index.xhtml as the welcome file, you should also have mapped the FacesServlet to that extension.

If you don't need the existing prefix mapping, replace that by:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

Otherwise just add the *.xhtml pattern meaning you'll end up with:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

Note that you nearly always want the *.xhtml mapping there in order to prevent exposing the source code of your Facelets to the public.

Update:

About the difference between .jsf and .xhtml:

.jsf is one of the default mappings (if you don't declare the FacesServlet at all in JSF 2.1, you'll get this among others). It stems from the difference between the extension the physical file on disk has (.jsp or .xhtml) and the extension used in the URL.

Unfortunately these were historically different since JSF was a kind of add-on technique that had to go via a separate explicit Servlet (the Faces Servlet).Pure JSP never had this limitation since it was more build-in. JSF pages were authored only in JSP at first, so if you just used .jsp in your URL requests, the container would go directly to JSP and skip the Faces Servlet.

So a workaround had to be invented for this, and that was to map the Faces Servlet to *.jsf, and then the Faces Servlet would direct to the JSP file, which still had the extension .jsp on disk.

Personally I think a simple *.xhtml to *.xhtml mapping is the only sane out-of-the-box option, but due to backwards compatibility it seems JSF can not set this as a default anymore.

这篇关于PrimeFaces不渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆