最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to change the resources folder name which is used by h:outputStylesheet and h:outputScript - Stack O

programmeradmin1浏览0评论

Is it necessary to give the folder name "resources" when making the reference of a css file and js file in h:outputStylesheet and h:outputScript?

Cause when I am giving the folder name "assets" instead of "resources" these files are not loaded.

This is how I am using these tags:

<h:head>
    <h:outputStylesheet name="css/styles.css"/>
    <h:outputScript name="js/site.js"/>
</h:head>

And here is my project structure:

In the screenshot you can see the folder name is resources. And then it is working. How can I specify a different name?

Is it necessary to give the folder name "resources" when making the reference of a css file and js file in h:outputStylesheet and h:outputScript?

Cause when I am giving the folder name "assets" instead of "resources" these files are not loaded.

This is how I am using these tags:

<h:head>
    <h:outputStylesheet name="css/styles.css"/>
    <h:outputScript name="js/site.js"/>
</h:head>

And here is my project structure:

In the screenshot you can see the folder name is resources. And then it is working. How can I specify a different name?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 2, 2012 at 17:20 Tapas BoseTapas Bose 29.8k78 gold badges220 silver badges338 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

The folder name is fixed as per chapters 2.6.1.1 and 2.6.1.2 of the JSF specification.

2.6.1.1 Packaging Resources into the Web Application Root

The default implementation must support packaging resources in the web application root under the path

resources/<resourceIdentifier>

relative to the web app root. Resources packaged into the web app root must be accessed using the getResource*() methods on ExternalContext.

2.6.1.2 Packaging Resources into the Classpath

For the default implementation, resources packaged in the classpath must reside under the JAR entry name:

META-INF/resources/<resourceIdentifier>

Resources packaged into the classpath must be accessed using the getResource*() methods of the ClassLoader obtained by calling the getContextClassLoader() method of the current Thread.

It mentions "must" in both cases. It does not mention anything about a possible configuration option to change those paths. This applies to both JSF 2.0 and JSF 2.1.

In the uping JSF 2.2, however, it will as per JSF spec issue 996 be possible to change the path by the new javax.faces.WEBAPP_RESOURCES_DIRECTORY context parameter which takes a path relative to the webcontent root as parameter value:

<context-param>
    <param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
    <param-value>WEB-INF/resources</param-value> 
</context-param>

This example will move the /resources folder into /WEB-INF, hereby ensuring more security (i.e. it is now not possible anymore to access those resources independently from the FacesServlet).

In your particular case, you'd thus like to use the following setting when having upgraded to JSF 2.2:

<context-param>
    <param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
    <param-value>assets</param-value> 
</context-param>

Note that this only covers webapp's own resources as specified in chapter 2.6.1.1, not the JAR resources as specified in chapter 2.6.1.2. The path of JAR resources should still be META-INF/resources as this is controlled by Servlet API specification, not the JSF specification. It's namely under the covers obtained by ServletContext#getResource() method which is outside control of JSF.

It is the way that the Resource Handler has been implemented in JSF 2.0, by default it will look at the following two path:

1) /resources under the root of the web application.

2) /META-INF/resources when packaged in an external JAR on the classpath.

If you want to use a different name, As mentioned by Alexandre, use the <script> and <style> tags or write your own Resource Handler.

More info:

  • Ryan Lubke's blog entry on JSF 2.0 Resource API

  • JSF 2.0 spec

You can't specify another directory than resources unless yiu are using

<style></style>

or

OmniFaces CombinedResourceHandler

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论