0

I have been trying to load resources (css, js and image files) using JSF 2.2's contracts in my maven project, but I get nothing but "unable to load" messages.

Here is the file structure:

src/main/webapp/*
src/main/contracts/library-name/css/*
src/main/contracts/library-name/js/*
src/main/contracts/library-name/template.xhtml
src/main/webapp/dev/index.xhtml

In my faces-config.xml I have:

<resource-library-contracts>
     <contract-mapping>
        <url-pattern>/dev/*</url-pattern>
    <contracts>library-name</contracts>
    </contract-mapping>
</resource-library-contracts>

In my index.xhtml I have tried the following:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <f:view contracts="library-name">
        <ui:composition template="/template.xhtml">
            <ui:define name="content">
                <p>text</p> 
            </ui:define> 
    </ui:composition>
    </f:view>
</html>

Here is the template file:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <h:outputScript name="js/script.js" />
        <h:outputStylesheet name="css/style.css" />
    </h:head>
    <h:body>
        <h1>Test</h1>
        <ui:insert name="content"/>
    </h:body>
</html>

EDIT: Here is the pom.xml file:

<?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.Demos</groupId>
    <artifactId>MyProject</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>MyProject</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 wanted to write everything I tried, but that is literally dozens of combinations of code, like using the h:outputScript h:outputStylesheet from within the ui:content definition, using plain HTML with an EL reference, removing the ui:composition and just using one page etc.

I have already tried putting contracts in the src/main/webapp/contracts folder, and had the same problem.

My output of HTML is always correct, but the generated css link tag returns a "RES_NOT_FOUND" error and the script either returns the same or doesn't show up at all. I am trying to load a CSS/JS framework, so I really need a solution that treats all of these (literally thousands) of assets modularly. I haven't been able to find anything that works online, probably because contracts are fairly new, but what could be the problem? Do I have to use a .jsf url mapping for my pages? Is there anyway to load these resources from a JAR file in a way similar to how primefaces is loaded? Would it make more sense just to upload the assets on the server and use a direct html link, since they are all front-end assets anyway?

Any help is greatly appreciated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ivan G
  • 3
  • 1
  • 6
  • Move your resources int `src/main/resources` instead. – khmarbaise Jun 25 '14 at 07:21
  • Thanks for responding! I didn't mention this before because I was trying to keep the topic restricted to "contracts", but I have had similar issues with these resources when they are just in the resources folder. From what I can tell, any resource that's more than one directory below the resources folder (i.e. `resources/js/bootstrap` instead of just `resources/js`) causes this "unable to load" error also, which is why I'm wondering if it may be indicative of a larger issue. – Ivan G Jun 25 '14 at 17:53
  • Can you show your full pom file? – khmarbaise Jun 25 '14 at 19:28
  • 1
    Moved from answer to comment: See the [official tutorial for contracts](http://docs.oracle.com/javaee/7/tutorial/doc/jsf-facelets008.htm#BABHAHDF). The `contracts` dir should be in the `src/main/webapp` dir or in [`META-INF/contracts` on the classpath](http://jdevelopment.nl/jsf-22/#1142). You current have it in neither. – Paul Samsotha Jun 26 '14 at 04:47

0 Answers0