简体   繁体   English

如何在 JSF 中使用 Font Awesome 等 3rd 方 CSS 库? 浏览器找不到 CSS 文件中引用的字体文件

[英]How to use 3rd party CSS libraries such as Font Awesome with JSF? Browser can't find font files referenced in the CSS file

I'm trying to integrate Font Awesome in JSF.我正在尝试将Font Awesome集成到 JSF 中。

<h:outputStylesheet library="font-awesome" name="css/font-awesome.min.css" />

But the browser can't find the font files.但是浏览器找不到字体文件。 They appear as empty squares:它们显示为空方块:

在此处输入图片说明

I expected them to look like below:我希望它们看起来像下面这样:

在此处输入图片说明

How is this caused and how can I solve it?这是怎么引起的,我该如何解决?

The Font Awesome CSS file is by default referencing those font files via a relative path ../ like below:默认情况下,Font Awesome CSS 文件通过相对路径../引用这些字体文件,如下所示:

@font-face {
  font-family: 'FontAwesome';
  src: url('../fonts/fontawesome-webfont.eot?v=4.3.0');
  src: url('../fonts/fontawesome-webfont.eot?#iefix&v=4.3.0') format('embedded-opentype'),
       url('../fonts/fontawesome-webfont.woff2?v=4.3.0') format('woff2'),
       url('../fonts/fontawesome-webfont.woff?v=4.3.0') format('woff'),
       url('../fonts/fontawesome-webfont.ttf?v=4.3.0') format('truetype'),
       url('../fonts/fontawesome-webfont.svg?v=4.3.0#fontawesomeregular') format('svg');
  font-weight: normal;
  font-style: normal;
}

This will fail if the CSS file itself is requested on a different path.如果在不同的路径上请求 CSS 文件本身,这将失败。 The JSF <h:outputStylesheet> will do that if you specify the library attribute.如果您指定library属性,JSF <h:outputStylesheet>会这样做。 Moreover, the JSF will use a special /javax.faces.resource/* prefix pattern for those resources so that specifically the JSF resource handler will be invoked which allows customization freedom.此外,JSF 将为这些资源使用特殊的/javax.faces.resource/*前缀模式,以便专门调用 JSF 资源处理程序,从而允许自定义自由。 Detail can be found in What is the JSF resource library for and how should it be used?详细信息可以在 JSF 资源库的用途是什么以及应该如何使用中找到?

Provided a folder structure like below,提供如下文件夹结构,

WebContent
 |-- resources
 |    `-- font-awesome
 |         |-- css
 |         |    |-- font-awesome.css
 |         |    `-- font-awesome.min.css
 |         `-- fonts
 |              |-- fontawesome-webfont.eot
 |              |-- fontawesome-webfont.svg
 |              |-- fontawesome-webfont.ttf
 |              |-- fontawesome-webfont.woff
 |              `-- fontawesome-webfont.woff2
 :

And the Font Awesome CSS being included as below: Font Awesome CSS 包含如下:

<h:outputStylesheet library="font-awesome" name="css/font-awesome.min.css" />

Then you need to edit the CSS file as below to use #{resource} mapping in EL to reference the font files in /resources folder with the appropriate library and resource name (and as library name ends up as a query string parameter already, you also need to replace ? by & , this is not necessary if you don't use a library name).然后你需要编辑如下的 CSS 文件,以在 EL 中使用#{resource}映射来引用/resources文件夹中具有适当库和资源名称的字体文件(并且由于库名称已经作为查询字符串参数结束,你还需要将?替换为& ,如果您不使用库名称,则不需要这样做)。

@font-face {
  font-family: 'FontAwesome';
  src: url("#{resource['font-awesome:fonts/fontawesome-webfont.eot']}&v=4.3.0");
  src: url("#{resource['font-awesome:fonts/fontawesome-webfont.eot']}&#iefix&v=4.3.0") format('embedded-opentype'),
       url("#{resource['font-awesome:fonts/fontawesome-webfont.woff2']}&v=4.3.0") format('woff2'),
       url("#{resource['font-awesome:fonts/fontawesome-webfont.woff']}&v=4.3.0") format('woff'),
       url("#{resource['font-awesome:fonts/fontawesome-webfont.ttf']}&v=4.3.0") format('truetype'),
       url("#{resource['font-awesome:fonts/fontawesome-webfont.svg']}&v=4.3.0#fontawesomeregular") format('svg');
  font-weight: normal;
  font-style: normal;
}

Make sure you restart the server after editing the CSS file.确保在编辑 CSS 文件后重新启动服务器。 The presence of EL expressions in a certain CSS file is detected only once during the first time the JSF resource handler reads the CSS file and then remembered applicationwide.在 JSF 资源处理程序第一次读取 CSS 文件并随后在整个应用程序范围内记住时,在某个 CSS 文件中只检测到一次 EL 表达式。

In case you're seeing JSF1091 warnings in server logs for those font files like below:如果您在服务器日志中看到这些字体文件的 JSF1091 警告,如下所示:

WARNING: JSF1091: No mime type could be found for file [some font file].警告:JSF1091:找不到文件 [某些字体文件] 的 MIME 类型。 To resolve this, add a mime-type mapping to the applications web.xml.要解决此问题,请将 MIME 类型映射添加到应用程序 web.xml。

Then you need to act accordingly by adding below mime mappings to web.xml :然后,您需要通过将以下 MIME 映射添加到web.xml来采取相应措施:

<mime-mapping>
    <extension>eot</extension>
    <mime-type>application/vnd.ms-fontobject</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>otf</extension>
    <mime-type>font/opentype</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>svg</extension>
    <mime-type>image/svg+xml</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>ttf</extension>
    <mime-type>application/x-font-ttf</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>woff</extension>
    <mime-type>application/x-font-woff</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>woff2</extension>
    <mime-type>application/x-font-woff2</mime-type>
</mime-mapping>

If you happen to use JSF utility library OmniFaces , an alternative to editing the CSS file with the #{resource} mapping, is to install the OmniFaces UnmappedResourceHandler and reconfigure the FacesServlet mapping as per its documentation.如果您碰巧使用 JSF 实用程序库OmniFaces ,使用#{resource}映射编辑 CSS 文件的替代方法是安装 OmniFaces UnmappedResourceHandler并根据其文档重新配置FacesServlet映射。 You only need to make sure that you don't reference the font CSS file via library anymore:您只需要确保不再通过library引用字体 CSS 文件:

<h:outputStylesheet name="font-awesome/css/font-awesome.min.css" />

See also:也可以看看:

Also primefaces>5.11 has Font Awesome out of the box此外, primefaces>5.11具有开箱即用的Font Awesome

Just add this context-param to your web.xml :只需将此context-param添加到您的web.xml

<context-param>
    <param-name>primefaces.FONT_AWESOME</param-name>
    <param-value>true</param-value>
</context-param>

Then you can apply Font Awesome icons this way :然后你可以通过这种方式应用Font Awesome图标:

<p:submenu label="Time"  icon="fa fa-clock-o">

References :参考 :

I changed the path in "font-awesome.css" and work fine我改变了“font-awesome.css”中的路径并且工作正常

@font-face {
font-family: 'FontAwesome';
src: url('../resources/font-awesome-4.5.0/fonts/fontawesome-webfont.eot?v=4.5.0');
src: url('../resources/font-awesome-4.5.0/fonts/fontawesome-webfont.eot?#iefix&v=4.5.0') format('embedded-opentype'),
    url('../resources/font-awesome-4.5.0/fonts/fontawesome-webfont.woff2?v=4.5.0') format('woff2'),
    url('../resources/font-awesome-4.5.0/fonts/fontawesome-webfont.woff?v=4.5.0') format('woff'),
    url('../resources/font-awesome-4.5.0/fonts/fontawesome-webfont.ttf?v=4.5.0') format('truetype'),
    url('../resources/font-awesome-4.5.0/fonts/fontawesome-webfont.svg?v=4.5.0#fontawesomeregular')
    format('svg');
font-weight: normal;
font-style: normal;

} }

i changed the path of oet,ttf,svg,woff files in "font-awesome.min.css" file like this :我在“font-awesome.min.css”文件中更改了oet,ttf,svg,woff文件的路径,如下所示:

http://localhost:8080/Students_manager/javax.faces.resource/fontawesome-webfont.svg.xhtml?ln=font-awesome/fonts&v=4.2.0#fontawesomeregular

it's working fine for me but i still looking for another solution because i should have a dynamic path not a static like "http://localhost:8080/Students_manager/..."它对我来说很好,但我仍在寻找另一个解决方案,因为我应该有一个动态路径而不是像"http://localhost:8080/Students_manager/..."这样的静态路径

Same answer as BalusC with some changes.BalusC相同的答案,但有一些变化。 Note: I'm integrating Metronic Theme.注意:我正在集成 Metronic 主题。

*/@font-face{font-family:'FontAwesome';
 src:url("#{resource['global:plugins/font-awesome/fonts/fontawesome-webfont.eot']}&v=4.4.0");
 src:url("#{resource['global:plugins/font-awesome/fonts/fontawesome-webfont.eot']}&#iefix&v=4.4.0") format('embedded-opentype'),
 url("#{resource['global:plugins/font-awesome/fonts/fontawesome-webfont.woff2']}&v=4.4.0") format('woff2'),
 url("#{resource['global:plugins/font-awesome/fonts/fontawesome-webfont.woff']}&v=4.4.0") format('woff'),
 url("#{resource['global:plugins/font-awesome/fonts/fontawesome-webfont.ttf']}&v=4.4.0") format('truetype'),
 url("#{resource['global:plugins/font-awesome/fonts/fontawesome-webfont.svg']}&v=4.4.0#fontawesomeregular") format('svg');font-weight:normal;font-style:normal}

same goes for simple-line-icons.min.css simple-line-icons.min.css

and as BalusC said add change web.xml by adding the lines in his answer.正如 BalusC 所说,通过在他的答案中添加行来添加更改web.xml

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM