7

Using h:outputStylesheet I can embed CSS resources in the HTML head section, but how can I build a <link> for a favicon image resource which renders HTML like in this example:

HTML output:

<head>
... 
<link rel="icon" type="image/png" href="favicon.png" />
...
</head>

The image resource is located in <web>/resources/images.

If I use direct HTML code in the JSF template like href="/resources/images/favicon.png" the resource is not found - navigating to /resources/images/favicon.png leads to the error

/resources/images/favicon.png/index.jsf not found

(I have set index.jsf as index page in web.xml which might explain this path)

2 Answers 2

10

Your webapp is apparently running on a non-empty context path. The leading slash / brings you to the domain root. Use #{request.contextPath} to dynamically inline the context path.

<link rel="shortcut icon" type="image/png" href="#{request.contextPath}/resources/images/favicon.png" />

(note that I fixed the rel as well to make it crossbrowser compatible)

2

The href="/resources/images/favicon.png" is actually looking in the root direcotry of your server http://localhost/resources/images/favicon.png and not inside your web application directory.

Your href location will need to include the web application directory href="/webappname/resources/images/favicon.png" http://localhost/webappname/resources/images/favicon.png


If your .xhtml file is in the same directory as your resources folder then removing the forward slash at the being should work as well. href="resources/images/favicon.png"

2
  • 1
    the web app name can be set at deployment time, using it in a hard coded resource could be dangerous. In my example, the app is deployed at the server root http://localhost:8080/ directly
    – mjn
    Jun 16, 2011 at 14:55
  • @BalusC I don't mean in resources folder like this /web/index.xhtml and /web/resources/
    – Mark
    Jun 16, 2011 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.