未加载 Primefaces 资源

Primefaces resources are not loaded

我在 TomEE 中使用 Primefaces 5.3,但是当我访问我的页面时,PF 资源没有被加载,我收到以下错误:

$ is not defined
PrimeFaces is not defined

即使我访问资源 URL 我也收到以下错误 URL:

http://localhost:8080/Myapp/javax.faces.resource/theme.css.xhtml?ln=primefaces-aristo

错误:

XML Parsing Error: no root element found Location: http://localhost:8080/Myapp/javax.faces.resource/theme.css.xhtml?ln=primefaces-aristo Line Number 1, Column 1

我的页面:

<?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://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

<h:head>

</h:head>

<f:view>
    <p:growl id="notificationMessage" showDetail="true" />
    <p:panel id="basic" header="Basic" footer="Movie Details" style="margin-bottom:20px">
        <h:panelGrid columns="1" cellpadding="10">

            <p:outputLabel  for="mbbData" value="Data Value: " />
            <p:inputText id="mbbData" value="#{mbbDataInputController.mbbData}" required="true" />

            <p:commandButton action="#{mbbDataInputController.addMbb}" />


        </h:panelGrid>
    </p:panel>


</f:view>
</html>

web.xml:

<?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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID"
         version="3.0">

    <display-name>my-service</display-name>


    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>
            WEB-INF/faces-config.xml
        </param-value>
    </context-param>

    <!-- Faces Servlet -->
    <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>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>rest-servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.huawei.myapp.service</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>rest-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

我还尝试在页眉中添加以下内容:

<h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />

但是没用。

另请注意,当我调试 PrimeResourceHandler class 时,正在创建资源并且资源路径在正确的 jar 中。

好的,我发现问题及解决方法如下

问题:

问题是我使用了两个 servlet,javax.faces.webapp.FacesServlet 用于 JSF,org.glassfish.jersey.servlet.ServletContainer 用于 REST Web 服务。当我请求资源文件时,实际上 Jersey servlet 会为我提供它而不是 faces servlet。

解法:

我将 web.xml url 映射更改为:

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