How to use Font Awesome from webjars.org with JSF

一曲冷凌霜 提交于 2019-11-26 16:28:45

The JSF mapping and library name is missing in those URLs. If you've mapped your FacesServlet on *.xhtml, then those URLs should actually have been:

GET http://DOMAIN:PORT/CONTEXT-ROOT/javax.faces.resource/font-awesome/3.2.1/font/fontawesome-webfont.woff.xhtml?ln=webjars&v=3.2.1
GET http://DOMAIN:PORT/CONTEXT-ROOT/javax.faces.resource/font-awesome/3.2.1/font/fontawesome-webfont.ttf.xhtml?ln=webjars&v=3.2.1
GET http://DOMAIN:PORT/CONTEXT-ROOT/javax.faces.resource/font-awesome/3.2.1/font/fontawesome-webfont.svg.xhtml?ln=webjars

Essentially, you should be using #{resource} in CSS file to print the proper JSF resource URL:

src: url("#{resource['webjars:font-awesome/3.2.1/font/fontawesome-webfont.eot']}&v=3.2.1");
src: url("#{resource['webjars:font-awesome/3.2.1/font/fontawesome-webfont.eot']}&#iefix&v=3.2.1");

However, as the source code is actually outside your control (you can't edit it), then there's no other way to manage the resource handling yourself. The JSF utility library OmniFaces provides the UnmappedResourceHandler out the box for the exact purpose. With the following steps your problem should be solved:

  1. Install OmniFaces, it's available on Maven as well.

    <dependency>
        <groupId>org.omnifaces</groupId>
        <artifactId>omnifaces</artifactId>
        <version><!-- Check omnifaces.org for current version. --></version>
    </dependency>
    
  2. Register UnmappedResourceHandler in faces-config.xml as follows:

    <application>
        <resource-handler>org.omnifaces.resourcehandler.UnmappedResourceHandler</resource-handler>
    </application>
    
  3. Add /javax.faces.resource/* to FacesServlet mapping, assuming that the servlet name is facesServlet and you've already a mapping on *.xhtml:

    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>/javax.faces.resource/*</url-pattern>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    
  4. Move the <h:outputStylesheet> library name to into the resource name.

    <h:outputStylesheet name="webjars/font-awesome/3.2.1/css/font-awesome.css" />
    
  5. Profit.

The answer above is kind of made obsolete. Since some releases ago, the webjar version of font-awesome includes a specific jsf-ified version of the css so there is nothing to configure. Add the jar to your project, either via maven

<dependency>
   <groupId>org.webjars</groupId>
   <artifactId>font-awesome</artifactId>
   <version>4.6.3</version>
</dependency>

or directly and it just works. Just make sure you include the correct css

<h:outputStylesheet library="webjars" name="font-awesome/4.6.3/css/font-awesome-jsf.css" />

Note the -jsf in the name!!! This way you can always have the latest version in your application and do not depend on PF releasing something new

Hatem Alimam

In addition to BalusC answer, it's a good idea to add the following mime-mappings to the web.xml

<!-- web fonts -->
<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>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>
<mime-mapping>
    <extension>svg</extension>
    <mime-type>image/svg+xml</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>ico</extension>
    <mime-type>image/x-icon</mime-type>
</mime-mapping>

In addition to BalusC and Hatem Alimam answers, this could be useful too by adding:

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

According to this link

For primefaces 6.2 also this worked fine for me

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>font-awesome</artifactId>
        <version>5.5.0</version>
    </dependency>

and in xhtml file:

<h:outputScript library="webjars" name="font-awesome/5.5.0/js/all.js"/>

be aware, that you have to change the usage from 4 to 5, for example fa fa-plus to fas fa-plus, based on web page - https://fontawesome.com/icons/plus?style=solid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!