我有一个使用maven在Glassfish 4.0中运行的Javaee7,JSF2 Web应用程序。
我的所有资源都在/src/main/webapp/resources
文件夹中,但由于我使用的是框架,因此它们有时会嵌套在多个子文件夹中(即resources/theme/plugin/bootstrap/script.js
)。
存储在资源文件夹下的一个目录中的CSS,JS和图像文件(即resources/theme/custom.css
)可以使用以下代码加载:
<h:outputScript library="theme" name="custom.js" />
但是如果我尝试加载位于子文件夹中的资源,它就不起作用:
<h:outputScript library="theme" name="plugin/bootstrap/script.js" />
我也试过这个:
<h:outputScript name="theme/plugin/bootstrap/script.js" />
和
<h:outputScript name="plugin/bootstrap/script.js" />
但是一切都返回“无法加载资源”。
这是我的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>
如果有帮助,这里是pom.xml
<?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>
我检查是否存在可能阻止访问资源的安全约束,但所有这些都属于某个xhtml文件的文件夹,所以我怀疑这是个问题。例如,我的大部分安全限制都是/admin/*
或/user/*
等。
任何帮助将不胜感激!谢谢!
答案 0 :(得分:0)
您需要符合JSF的资源库命名结构,或者放弃完全嵌套的目录结构
JSF有点复杂的资源库命名约定正在弄乱你。根据JSF规范,将以下模板视为构成有效资源组件路径的内容:
resources/<resourceIdentifier>
<resourceIdentifier>
受以下结构约束:
[localePrefix/][libraryName/][libraryVersion/]resourceName[/resourceVersion]
以下是规则:
如果存在libraryVersion,则必须以libraryName开头
如果存在libraryVersion,则必须忽略libraryName下的所有叶子文件。
如果存在resourceVersion,则必须以resourceName
/
如果存在libraryVersion或resourceVersion,则两者都必须是_
分隔的整数列表,不能以_
开头也不是
如果存在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/]resourceName[/resourceVersion]
。根据规则,该路径的几个位不符合成功评估资源路径的标准
答案 1 :(得分:0)
我遇到了类似的问题,因为我想将jquery脚本保存在一个单独的文件夹中
我通过将子文件夹添加到库名称来实现它:
<h:outputScript library="script/jquery" name="jquery.js"/>
但我不知道这有多可靠。