【问题标题】:Primefaces renders without css styles?Primefaces 渲染没有 CSS 样式?
【发布时间】:2016-09-01 20:06:24
【问题描述】:

我浏览了有关 primefaces not rendering 的主题,但找不到与我匹配的案例。

因此,当我们以 commanButton 为例时,按钮呈现但看起来就像默认的 jsf 按钮

我猜可能是因为css没有加载?

这是代码中的样子:

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

</h:head>
<h:body>
    <p:commandButton value="Example button" />
    .
    ..
    ...
</h:body>
</html>

primefaces 库肯定已下载 - 我可以在外部库部分看到它:

这是我的 pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Catering</groupId>
  <artifactId>Catering</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Catering Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
    <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-api</artifactId>
  <version>7.0</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.2.1.Final</version>
</dependency>
<dependency>
  <groupId>postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.1-901-1.jdbc4</version>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.10</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.primefaces</groupId>
  <artifactId>primefaces</artifactId>
  <version>6.0</version>
</dependency>
  </dependencies>
  <build>
     <finalName>Catering</finalName>
  </build>
 </project>

这就是我在 web.xml 中配置它的方式:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
<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>
...
..
.

</web-app>

你有什么想法?如果您需要更多源代码,请告诉我。

【问题讨论】:

  • 您是否通过浏览器开发工具检查网络流量?
  • 原来与 web.xml 中定义的安全约束有关。当我删除这部分时,它出乎意料地起作用。当我完全解决它时,我会描述它。

标签: jsf jsf-2 primefaces


【解决方案1】:

好的,所以问题是因为我对所有文件都设置了安全约束

<security-constraint>
    <display-name>pages_auth</display-name>

    <web-resource-collection>
        <web-resource-name>pages_auth</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>

    <auth-constraint>
        <role-name>user</role-name>
    </auth-constraint>
</security-constraint>

然后当尝试访问受保护的资源(也指 css 文件)时,primefaces 不会在登录页面上正确呈现。这里描述了如何解决它: PrimeFaces not rendering when using login form authentication

【讨论】:

  • 首先,在答案中你没有说它应该如何配置工作。你有一个工作的例子吗?其次,这不是错误,而是完全按照您告诉它的行为。如果 css 等位于安全 url 后面,则它们无法加载到非安全页面上。纯html也会有问题。第三,问题的 web.xml 中缺少这部分。下次创建一个minimal reproducible example,你会很快找到这个
  • 是的,感谢您的建议,我不认为安全可以在这里发挥作用。
  • 仍然,您的答案不包含解决方案...您可以增强它吗?
  • 目前我无法真正重现此错误以深入研究该主题,但一般准则是注意您保护的资源,尝试排除 css、js 和 jpg 文件,如建议链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 2014-06-10
  • 2013-02-02
  • 2017-10-12
  • 1970-01-01
  • 2012-12-29
  • 2012-07-04
相关资源
最近更新 更多