java PrimeFaces 不渲染

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11324700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:40:12  来源:igfitidea点击:

PrimeFaces Not Rendering

javajsfprimefaces

提问by Alvin Sim

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.

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

I have tried creating the XHTML file by:

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

  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.
  1. 选择项目,右键单击 New.. 并选择 Other.. 并选择“Web”类别,然后选择 JSF Page。
  2. 选择项目,右键单击 New.. 并选择 Other.. 并选择“JavaServer Faces”类别,然后选择 JSF Page。
  3. 选择项目,右键单击New.. 并选择Other.. 并选择“Other”类别,然后选择XHTML Page。

None of the methods above worked.

以上方法均无效。

Maven POM

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

<?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

索引.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.

这是第一个场景。第二个场景,我已经有一个 NetBeans Web 应用程序项目,可以使用 PrimeFaces 正常运行。“index.html”确实呈现。但是当我创建一个新的 JSF 页面并包含 PrimeFaces 命名空间时xmlns:p="http://primefaces.org/ui",但是这个特定的 JSF 和其他新创建的 JSF 页面不会呈现。

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)
  • JDK6
  • Netbeans 7.1.2
  • PrimeFaces 3.3
  • 阿帕奇7.0.2
  • Windows 7(32 位)

Thanks.

谢谢。

回答by Arjan Tijms

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

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

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 *.xhtmlpattern meaning you'll end up with:

否则只需添加*.xhtml模式意味着你最终会得到:

<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 *.xhtmlmapping there in order to prevent exposing the source code of your Facelets to the public.

请注意,您几乎总是希望在*.xhtml那里进行映射,以防止将您的 Facelets 的源代码暴露给公众。

Update:

更新:

About the difference between .jsf and .xhtml:

关于.jsf和.xhtml的区别:

.jsfis 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.

.jsf是默认映射之一(如果您在 JSF 2.1 中根本没有声明 FacesServlet,那么您将得到它)。它源于磁盘上物理文件的扩展名(.jsp 或 .xhtml)与 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.

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

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

Personally I think a simple *.xhtmlto *.xhtmlmapping 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.

我个人认为简单*.xhtml*.xhtml映射是唯一合理的开箱即用选项,但由于向后兼容性,JSF 似乎不能再将其设置为默认值。

回答by user3877090

try to put the primefaces.jar file under WebContent/WEB-INF/lib then add this jar to the build path

尝试将 primefaces.jar 文件放在 WebContent/WEB-INF/lib 下,然后将此 jar 添加到构建路径

回答by u?6??n? ???u?p

Add another servlet-mapping:

添加另一个 servlet 映射:

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

After that use the following Url: http://[HOST]:[PORT]/[WEBAPP]/faces/index.jsf(.jsfand NOT .xhtml).

之后使用以下网址:http://[HOST]:[PORT]/[WEBAPP]/faces/index。jsf.jsf而不是 .xhtml)。