JSF无法在子文件夹中找到资源 [英] JSF is unable to find resources in sub-folders

查看:112
本文介绍了JSF无法在子文件夹中找到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的所有资源都放在我的 / src / main / webapp / resources 文件夹,但因为我使用的是框架,所以它们有时嵌套在多个子文件夹中(即 resources / theme / plugin / bootstrap / script.js )。

资源文件夹下的一个目录下存储的CSS,JS和图像文件(即 resources / theme / custom.css

 < h:outputScript library =themename =custom。 js/> 

但是,如果我尝试加载位于子文件夹中的资源,它不起作用:

 < h:outputScript library =themename =plugin / bootstrap / script.js/> 

我也试过这个:

 < h:outputScript name =theme / plugin / bootstrap / script.js/> 

 < h:outputScript name =plugin / bootstrap / script.js/> 

但一切都返回无法加载资源。

这是我的web.xml的开始

 <?xml version =1.0encoding =UTF -8\" >?; 
< web-app version =3.1xmlns =http://xmlns.jcp.org/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi:schemaLocation =http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd\">
< context-param>
< param-name> javax.faces.PROJECT_STAGE< / param-name>
<参数值>开发< /参数值>
< / context-param>
< context-param>
< param-name> javax.faces.WEBAPP_RESOURCES_DIRECTORY< / param-name>
<参数值> /资源< /参数值>
< / context-param>
< context-param>
< param-name> javax.faces.FACELETS_SKIP_COMMENTS< / param-name>
< param-value> true< /参数值>
< / context-param>
<! - 设定网路应用程式的预设时区 - >
< context-param>
< / param-name> javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE< / param-name>
< param-value> true< /参数值>
< / context-param>
< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
<加载启动> 1< /加载启动>
< / servlet>
< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> *。xhtml< / url-pattern>
< / servlet-mapping>

这里是pom.xml,如果有帮助的话

 <?xml version =1.0encoding =UTF-8?> 
< project xmlns =http://maven.apache.org/POM/4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
< modelVersion> 4.0.0< / modelVersion>

< groupId> com.WebStore< / groupId>
< artifactId> MyApp< / artifactId>
< version> 1.0-SNAPSHOT< / version>
<包装>战争< / packaging>

< name> MyApp< / name>

<属性>
< endorsed.dir> $ {project.build.directory} /endorsed</endorsed.dir>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< / properties>

<依赖关系>
< dependency>
< groupId> javax.mail< / groupId>
< artifactId>邮件< / artifactId>
< version> 1.4.3< / version>
< /依赖关系>
< dependency>
< groupId> net.coobird< / groupId>
< artifactId>缩略图< / artifactId>
< version> [0.4,0.5)< / version>
< /依赖关系>
< dependency>
< groupId> org.omnifaces< / groupId>
< artifactId> omnifaces< / artifactId>
< version> 1.7< / version>
< /依赖关系>
< dependency>
< groupId> commons-fileupload< / groupId>
< artifactId> commons-fileupload< / artifactId>
< version> 1.2.1< / version>
< /依赖关系>
< dependency>
< groupId> commons-io< / groupId>
< artifactId> commons-io< / artifactId>
< version> 1.4< / version>
< /依赖关系>
< dependency>
< groupId> com.google.guava< / groupId>
< artifactId>番石榴< / artifactId>
< version> 16.0.1< / version>
< /依赖关系>
< dependency>
< groupId> org.eclipse.persistence< / groupId>
< artifactId> eclipselink< / artifactId>
< version> 2.5.1< / version>
< scope>提供< / scope>
< /依赖关系>
< dependency>
< groupId> org.eclipse.persistence< / groupId>
< artifactId> org.eclipse.persistence.jpa.modelgen.processor< / artifactId>
< version> 2.5.1< / version>
< scope>提供< / scope>
< /依赖关系>
<! - <依赖关系>
< groupId> org.glassfish.main.extras< / groupId>
< artifactId> glassfish-embedded-all< / artifactId>
< version> 4.0< / version>
< /依赖关系> - >
< dependency>
< groupId> org.apache.derby< / groupId>
< artifactId> derby< / artifactId>
< version> 10.10.1.1< / version>
< /依赖关系>
< dependency>
< groupId> javax< / groupId>
< artifactId> javaee-web-api< / artifactId>
< version> 7.0< / version>
< scope>提供< / scope>
< /依赖关系>
< dependency>
< groupId> org.primefaces.extensions< / groupId>
< artifactId> primefaces-extensions< / artifactId>
< version> 1.2.1< / version>
< /依赖关系>
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.11< / version>
< /依赖关系>
< dependency>
< groupId> com.paypal.sdk< / groupId>
< artifactId> rest-api-sdk< / artifactId>
< version> 0.5.2< / version>
< /依赖关系>
< /依赖关系>

< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 3.1< / version>
<配置>
< source> 1.7< / source>
< target> 1.7< / target>
< compilerArguments>
< endorseddirs> $ {endorsed.dir}< / endorseddirs>
< / compilerArguments>
< / configuration>
< / plugin>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-surefire-plugin< / artifactId>
< version> 2.12.4< / version>
<配置>
< skipTests>真< / skipTests>
< / configuration>
< / plugin>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-war-plugin< / artifactId>
< version> 2.3< / version>
<配置>
< failOnMissingWebXml> false< / failOnMissingWebXml>
< / configuration>
< / plugin>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-dependency-plugin< / artifactId>
< version> 2.6< / version>
<执行次数>
<执行>
<阶段>验证< /阶段>
<目标>
< goal>复制< / goal>
< /目标>
<配置>
< outputDirectory> $ {endorsed.dir}< / outputDirectory>
< silent> true< / silent>
< artifactItems>
< artifactItem>
< groupId> javax< / groupId>
< artifactId> javaee-endorsed-api< / artifactId>
< version> 7.0< / version>
< type> jar< / type>
< / artifactItem>
< / artifactItems>
< / configuration>
< /执行>
< /执行次数>
< / plugin>
< / plugins>
< / build>

< / project>

我检查是否存在可能阻止访问资源的安全限制,但是所有它们属于某个xhtml文件的文件夹,所以我怀疑这是个问题。例如,我的大多数安全限制都是 / admin / * / user / * 等。



任何帮助将不胜感激!谢谢!

解决方案

您需要符合JSF的资源库命名结构,或放弃所有嵌套的目录结构






JSF有点复杂的资源库命名约定与您混淆了。根据JSF规范,请考虑以下模板作为构成有效资源组件路径的内容:

  resources /< resourceIdentifier> 

其中< resourceIdentifier> 受到以下结构:

pre $ localePrefix / [libraryName /] [libraryVersion /] resourceName [/ resourceVersion]

以下是规则:


  1. 如果libraryVersion存在,它必须以libraryName为前缀。

  2. 如果libraryVersion存在,libraryName下的任何叶文件都必须被忽略。

    >
  3. 如果存在resourceVersion,它必须以resourceName开头


  4. 必须存在<$

  5. 如果存在libraryVersion或resourceVersion,则两者必须为<$ c $ 以 _

  6. 开头或结尾
  7. p>如果存在resourceVersion,它必须是与libraryVersion格式相同的版本号。可选的文件扩展名可以与resourceVersion一起使用。如果使用文件扩展名,则必须在版本号后附加一个。字符,后跟一个文件扩展名。

例如, resources / basic / 2_3 / script.js / 1_3_4.js 满足有效资源路径的要求



根据以上规则:


  • < resources> /theme/custom.css 很好,并正确评估: [libraryName] / resourceName


  • < resources> /theme/plugin/bootstrap/script.js 将失败,因为它的计算方式如下: [libraryName /] [ libraryVersion /]资源名称[/ resourceVersion] 。根据这些规则,该路径的几个位未能成功评估资源路径的标准



I have a Javaee7, JSF2 web app running in Glassfish 4.0 using maven.

I have all of my resources in my /src/main/webapp/resources folder, but because I am using a framework, they are sometime nested in multiple subfolders, (i.e. resources/theme/plugin/bootstrap/script.js).

The CSS, JS and image files that are stored one directory below the resources folder (i.e. resources/theme/custom.css) would load fine with this code:

<h:outputScript library="theme" name="custom.js" />  

But if I try to load a resource that's located in sub-folders, it doesn't work:

<h:outputScript library="theme" name="plugin/bootstrap/script.js" /> 

I've also tried this:

<h:outputScript name="theme/plugin/bootstrap/script.js" /> 

and

<h:outputScript name="plugin/bootstrap/script.js" /> 

But everything returns "Unable to load resource".

Here is the start of my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
        <param-value>/resources</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <!-- sets the default time zone for the web app -->
    <context-param>
        <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
        <param-value>true</param-value>
    </context-param>
    <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>

And here is the pom.xml if that helps

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.WebStore</groupId>
<artifactId>MyApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>MyApp</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
         <groupId>javax.mail</groupId>
         <artifactId>mail</artifactId>
         <version>1.4.3</version>
    </dependency>
    <dependency>
        <groupId>net.coobird</groupId>
        <artifactId>thumbnailator</artifactId>
        <version>[0.4, 0.5)</version>
      </dependency>
    <dependency>
        <groupId>org.omnifaces</groupId>
        <artifactId>omnifaces</artifactId>
        <version>1.7</version>
    </dependency>
    <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
    </dependency>
    <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>16.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.1</version>
        <scope>provided</scope>
    </dependency>
    <!--<dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>4.0</version>
    </dependency>-->
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.10.1.1</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.primefaces.extensions</groupId>
        <artifactId>primefaces-extensions</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>com.paypal.sdk</groupId>
        <artifactId>rest-api-sdk</artifactId>
        <version>0.5.2</version>
    </dependency>     
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

</project>

I checked to see if there are security constraints that may prevent the access to the resources, but all of them pertain to a certain folder of xhtml files, so I doubt that is the issue. For example, most of my security constraints are /admin/* or /user/* etc.

Any help would be appreciated! thanks!

解决方案

You'll either need to conform to JSF's resource library naming structure or abandon the nested directory structure you have altogether


JSF's somewhat complicated resource library naming convention is messing with you. Consider the following template as what constitutes a valid resource component path, per the JSF specification:

resources/<resourceIdentifier>

Where <resourceIdentifier> is bound by the following structure:

[localePrefix/][libraryName/][libraryVersion/]resourceName[/resourceVersion]

Here are the rules:

  1. If libraryVersion is present, it must be preceded by libraryName

  2. If libraryVersion is present, any leaf files under libraryName must be ignored.

  3. If resourceVersion is present, it must be preceded by resourceName

  4. There must be a / between adjacent segments in a

  5. If libraryVersion or resourceVersion are present, both must be a _ separated list of integers, neither starting nor ending with _

  6. If resourceVersion is present, it must be a version number in the same format as libraryVersion. An optional "file extension" may be used with the resourceVersion. If "file extension" is used, a "." character, followed by a "file extension" must be appended to the version number.

As an example, resources/basic/2_3/script.js/1_3_4.js fulfils the requirements for a valid resource path

According to the rules above:

  • <resources>/theme/custom.css is fine and evaluates properly: [libraryName]/resourceName

  • <resources>/theme/plugin/bootstrap/script.js will fail because it's evaluated as: [libraryName/][libraryVersion/]resourceName[/resourceVersion]. According to the rules, several bits of that path fail the criteria for successful evaluation of the resource path

这篇关于JSF无法在子文件夹中找到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆