Hi,
That is all true. However, #{resource['...']} gives you a path like
/context-root/faces/javax.faces.resource/resourceName.suffix, thus a
URL for the browser. In <ui:composition template="..."> you need the
path to the file on the server! And this is something else entirely!
Regards,
Jakob
2011/2/19 Younes Ouadi <[email protected]>:
> Hi Jakob,
> Many thanks for your prompt feedback.
> In fact, I have understood from 'JEE 6 Tutorial' that JSF implementations
> are expected to manage templates like CSS and JS resources. Please refer to
> the bold parts of below section extracted from the Tutorial page 118:
> Web resources are any software artifacts that the web application requires
> for proper rendering, including images, script files, and any user-created
> component libraries. Resources must be collected in a standard location,
> which can be one of the following.
> ■ A resource packaged in the web application root must be in a subdirectory
> of a resources directory at the web application root:
> resources/resource-identifier.
> ■ A resource packaged in the web application’s classpath must be in a
> subdirectory of the META-INF/resources directory within a web
> application: META-INF/resources/resource-identifier.
> The JavaServer Faces runtime will look for the resources in the preceding
> listed locations, in that order.
> Resource identifiers are unique strings that conform to the following
> format: [locale-prefix/][library-name/][library-version/]resource-name[/resource-version]. Elements
> of the resource identifier in brackets ([]) are optional, indicating that
> only a resource-name, which is usually a file name, is a required element.
> Resources can be considered as a library location. Any artifact, such as a
> composite component or a template that is stored in the resources directory,
> becomes accessible to the other application components, which can use it to
> create a resource instance.
> END OF JEE 6 TUTORIAL
>
> From the above, I was expecting that MyFaces would found my resource
> 'templateInClasspath.xhtml'.
> Anyway, I will use your proposed solution as a workaround for the time
> being.
> Warm regards.
>
> Younes Ouadi
>
> On Sat, Feb 19, 2011 at 12:12 PM, Jakob Korherr <[email protected]>
> wrote:
>>
>> Hi Younes,
>>
>> Unfortunately #resource['...'] does not work that way. You should only
>> use it for getting the browser path to HTML resources like css or
>> javascript files, e.g.:
>>
>> <link href="#resource['lib:style.css']" rel="stylesheet" />
>>
>>
>> In your example you need to use Resource.getUrl() in a managed bean,
>> so something like that:
>>
>> <ui:composition template="#{myBean.templatePath}">
>>
>>
>> public String getTemplatePath()
>> {
>> FacesContext facesContext = FacesContext.getCurrentInstance();
>>
>> Resource resource =
>> facesContext.getApplication().getResourceHandler()
>> .createResource("templateInClasspath.xhtml", "mytempl");
>> URL url = resource.getURL();
>>
>> return url.toExternalForm();
>> }
>>
>> I hope this helps.
>>
>> Regards,
>> Jakob
>>
>>
>> 2011/2/19 Younes Ouadi <[email protected]>:
>> > Hello deras,
>> >
>> > I have a hands-on experience with JSF. I have been able to develop a
>> > first
>> > application. And I'm now in the stage of reorganizing it for a better
>> > management. I'm trying to move 'templates' to classpath in order to
>> > share
>> > them in jar for different applications. But I'm stack there. Pages that
>> > reference that template are not rendered and MyFaces returns the error
>> > (see
>> > below for full details of the error): java.io.FileNotFoundException:
>> >
>> > /lab-jsf-rsrcmng/javax.faces.resource/templateInClasspath.xhtml.xhtml?ln=mytempl
>> > Not Found in ExternalContext as a Resource
>> >
>> > Please help in pointing the missing part in my application.
>> >
>> > *1) Project:*
>> > *1.1) Hierarchy*
>> > lab-jsf-rsrcmng
>> > * pom.xml*
>> > src/main/webapp
>> > * home2.xhtml*
>> > WEB-INF
>> > * faces-config.xml*
>> > * web.xml*
>> > src/main/resources/META-INF/resources/mytempl
>> > * templateInClasspath.xhtml*
>> > *
>> > *
>> > *1.2) pom.xml*
>> > <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>lab-jsf</groupId>
>> > <artifactId>lab-jsf-rsrcmng</artifactId>
>> > <packaging>war</packaging>
>> > <version>0.0.1-SNAPSHOT</version>
>> >
>> > <name>LAB::JSF::RESOURCE MANAGEMENT</name>
>> > <url>http://maven.apache.org</url>
>> >
>> > <properties>
>> > <servlet.version>2.5</servlet.version>
>> > <jsp.version>2.1</jsp.version>
>> > <myfaces.version>2.0.3</myfaces.version>
>> > </properties>
>> >
>> > <dependencies>
>> > <dependency>
>> > <groupId>javax.servlet</groupId>
>> > <artifactId>servlet-api</artifactId>
>> > <version>${servlet.version}</version>
>> > <scope>provided</scope>
>> > </dependency>
>> > <dependency>
>> > <groupId>javax.servlet.jsp</groupId>
>> > <artifactId>jsp-api</artifactId>
>> > <version>${jsp.version}</version>
>> > <scope>provided</scope>
>> > </dependency>
>> > <dependency>
>> > <groupId>org.apache.myfaces.core</groupId>
>> > <artifactId>myfaces-api</artifactId>
>> > <version>${myfaces.version}</version>
>> > </dependency>
>> > <dependency>
>> > <groupId>org.apache.myfaces.core</groupId>
>> > <artifactId>myfaces-impl</artifactId>
>> > <version>${myfaces.version}</version>
>> > </dependency>
>> > <dependency>
>> > <groupId>junit</groupId>
>> > <artifactId>junit</artifactId>
>> > <version>4.8.2</version>
>> > <scope>test</scope>
>> > </dependency>
>> > </dependencies>
>> >
>> > <build>
>> > <plugins>
>> > <plugin>
>> > <groupId>org.apache.maven.plugins</groupId>
>> > <artifactId>maven-compiler-plugin</artifactId>
>> > <version>2.0.2</version>
>> > <configuration>
>> > <source>1.5</source>
>> > <target>1.5</target>
>> > </configuration>
>> > </plugin>
>> > </plugins>
>> > <finalName>${project.artifactId}</finalName>
>> > </build>
>> > </project>
>> >
>> > *1.3) home2.xhtml*
>> > <?xml version="1.0" encoding="UTF-8"?>
>> >
>> > <!DOCTYPE
>> > html PUBLIC
>> > "-//W3C//DTD XHTML 1.0 Transitional//EN"
>> > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>> >>
>> >
>> > <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:body>
>> > <ui:composition
>> > template="#{resource['mytempl:templateInClasspath.xhtml']}">
>> > </ui:composition>
>> > </h:body>
>> > </html>
>> >
>> >
>> >
>> > *1.4) faces-config.xml*
>> > <?xml version="1.0" encoding="UTF-8"?>
>> >
>> > <faces-config
>> > xmlns="http://java.sun.com/xml/ns/javaee"
>> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> > xsi:schemaLocation="
>> > http://java.sun.com/xml/ns/javaee
>> > http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd
>> > "
>> > version="2.0"
>> >>
>> > </faces-config>
>> >
>> >
>> > *1.5) web.xml:*
>> > <?xml version="1.0" encoding="UTF-8"?>
>> >
>> > <web-app
>> > version="2.5"
>> > xmlns="http://java.sun.com/xml/ns/javaee"
>> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> > xsi:schemaLocation="
>> > http://java.sun.com/xml/ns/javaee
>> > http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
>> > "
>> >>
>> > <display-name>lab-jsf-rsrcmng</display-name>
>> > <session-config>
>> > <session-timeout>30</session-timeout>
>> > </session-config>
>> > <welcome-file-list>
>> > <welcome-file>home2.xhtml</welcome-file>
>> > </welcome-file-list>
>> > <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>/faces/*</url-pattern>
>> > <url-pattern>*.xhtml</url-pattern>
>> > </servlet-mapping>
>> > <context-param>
>> > <description>
>> > If true, a javascript function will be rendered that is able to restore
>> > the
>> > former vertical scroll on every request. Convenient feature if you have
>> > pages
>> > with long lists and you do not want the browser page to always jump to
>> > the
>> > top
>> > if you trigger a link or button action that stays on the same page.
>> > Default is 'false'
>> > </description>
>> > <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
>> > <param-value>true</param-value>
>> > </context-param>
>> > <context-param>
>> > <param-name>org.apache.myfaces.USE_ENCRYPTION</param-name>
>> > <param-value>false</param-value>
>> > </context-param>
>> > </web-app>
>> >
>> >
>> > *1.6) templateInClasspath.xhtml*
>> > <!DOCTYPE
>> > html PUBLIC
>> > "-//W3C//DTD XHTML 1.0 Transitional//EN"
>> > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>> >>
>> >
>> > <html
>> > xmlns="http://www.w3.org/1999/xhtml"
>> > xmlns:c="http://java.sun.com/jsp/jstl/core"
>> > xmlns:f="http://java.sun.com/jsf/core"
>> > xmlns:h="http://java.sun.com/jsf/html"
>> > xmlns:ui="http://java.sun.com/jsf/facelets"
>> >>
>> > <f:view contentType="text/html">
>> > <h:head>
>> > <title>LAB::JSF::RSRCMNG</title>
>> > <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
>> > </h:head>
>> > <h:body styleClass="natisoft-body" >
>> > <h:panelGrid columns="1">
>> > <f:facet name="header">
>> > <ui:insert name="header">Header</ui:insert>
>> > </f:facet>
>> >
>> > <f:facet name="footer">
>> > <ui:insert name="trailer">Trailer</ui:insert>
>> > </f:facet>
>> >
>> > <h:panelGroup>
>> > <ui:insert name="content">Content</ui:insert>
>> > </h:panelGroup>
>> > </h:panelGrid>
>> > </h:body>
>> > </f:view>
>> > </html>
>> >
>> > *2) Error details*
>> >
>> > An Error Occurred:
>> > java.io.FileNotFoundException:
>> >
>> > /lab-jsf-rsrcmng/javax.faces.resource/templateInClasspath.xhtml.xhtml?ln=mytempl
>> > Not Found in ExternalContext as a Resource
>> >
>> > Caused by:
>> > java.io.FileNotFoundException -
>> >
>> > /lab-jsf-rsrcmng/javax.faces.resource/templateInClasspath.xhtml.xhtml?ln=mytempl
>> > Not Found in ExternalContext as a Resource
>> > - Stack Trace <http://localhost:17080/lab-jsf-rsrcmng/home2.xhtml#>
>> >
>> > javax.faces.FacesException: java.io.FileNotFoundException:
>> >
>> > /lab-jsf-rsrcmng/javax.faces.resource/templateInClasspath.xhtml.xhtml?ln=mytempl
>> > Not Found in ExternalContext as a Resource
>> > at
>> > org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.wrap(ExceptionHandlerImpl.java:241)
>> > at
>> > org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:156)
>> > at
>> > org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:258)
>> > at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
>> > at
>> > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
>> > at
>> > org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
>> > at
>> > org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
>> > at
>> > org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
>> > at
>> > org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
>> > at
>> > org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
>> > at
>> > org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
>> > at
>> > org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
>> > at
>> > org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
>> > at
>> > org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
>> > at
>> > org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
>> > at
>> > org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
>> > at
>> > java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
>> > at
>> > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
>> > at java.lang.Thread.run(Thread.java:619)
>> > Caused by: java.io.FileNotFoundException:
>> >
>> > /lab-jsf-rsrcmng/javax.faces.resource/templateInClasspath.xhtml.xhtml?ln=mytempl
>> > Not Found in ExternalContext as a Resource
>> > at
>> > org.apache.myfaces.view.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:199)
>> > at
>> > org.apache.myfaces.view.facelets.impl.DefaultFacelet.getRelativePath(DefaultFacelet.java:289)
>> > at
>> > org.apache.myfaces.view.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:346)
>> > at
>> > org.apache.myfaces.view.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:215)
>> > at
>> > org.apache.myfaces.view.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:140)
>> > at
>> > org.apache.myfaces.view.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:57)
>> > at
>> > org.apache.myfaces.view.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:45)
>> > at
>> > org.apache.myfaces.view.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:143)
>> > at
>> > org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.buildView(FaceletViewDeclarationLanguage.java:329)
>> > at
>> > org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:66)
>> > at
>> > org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
>> > ... 16 more
>> >
>> > - Component Tree <http://localhost:17080/lab-jsf-rsrcmng/home2.xhtml#>
>> > <UIViewRoot id="j_id0" inView="true" locale="en_US"
>> > renderKitId="HTML_BASIC"
>> > rendered="true" transient="false" viewId="/home2.xhtml"/> - State
>> > size:548
>> > bytes
>> > - Scoped Variables <http://localhost:17080/lab-jsf-rsrcmng/home2.xhtml#>
>> > Request ParametersNameValue*None*Request AttributesNameValue*None*View
>> > AttributesNameValue*None*Flash AttributesNameValue*None*Application
>> > AttributesNameValue*None*
>> > Feb 19, 2011 9:52:09 AM - Generated by MyFaces - for information on
>> > disabling or modifying this error-page, see Disabling error handling in
>> > MyFaces <http://wiki.apache.org/myfaces/Handling_Server_Errors>
>> >
>> > Thank you in advance
>> >
>> > Younes Ouadi
>> >
>>
>>
>>
>> --
>> Jakob Korherr
>>
>> blog: http://www.jakobk.com
>> twitter: http://twitter.com/jakobkorherr
>> work: http://www.irian.at
>
>
--
Jakob Korherr
blog: http://www.jakobk.com
twitter: http://twitter.com/jakobkorherr
work: http://www.irian.at