Problem Getting Started with Resource Loading in RF4.1
undermanager Jan 4, 2012 10:04 PMThis is a basic question about how to get started with resource loading in RF4.1. Apologies for the wordiness - but I wanted to show my working, as well as the problem I'm having.
QUESTION: What am I doing wrong in the following set-up? Why are my resources not getting packed/minified?
I am completely new to resource loading strategies in Java web apps - so this could be a problem between the keyboard and the chair.
1. BACKGROUND
I have recently upgraded from RF 4.0 to RF 4.1. I am trying to take advantage of the new compressing and packing (a.k.a. minification, I assume) features now available in 4.1.
So far, I've focused on packing/minification, but have not been able to get a working configuration.
The main components I'm using are:
- GlassFish 3.1.1
- RichFaces 4.1.0.Final
- YSlow (for feedback - what's packed/compressed; what's unpacked)
For the purposes of this exercise, I have the PROJECT_STAGE set to "Production" in the application's web.xml.
As an aside, Glassfish is configured to compress anything for the following mime types:
text/html,text/xml,text/plain,text/javascript,text/css,application/xml,application/xhtml+xml,application/javascript
The compression threshold, just for this exercise, is set to 128 bytes (usually I use the default value of 2048 bytes). I don't think that is relevant - but I mention it in case it is.
So, for example, using YSlow, I can see that:
- jquery.js is compressed from 238.1KB to 79.3KB
http://localhost:8080/dashboard/faces/javax.faces.resource/jquery.js
2. THE PROBLEM
I see 29 components that are not getting packed/minified, for example:
http://localhost:8080/dashboard/faces/javax.faces.resource/richfaces.js
http://localhost:8080/dashboard/faces/javax.faces.resource/toolbar.js?ln=org.richfaces
3. FIRST ATTEMPT
I look at chapter 5 of the 4.1.X Developer Guide, to see if I can pack/minify the above resources. I skip over section 5.4.2 and jump (maybe misguidedly) to section 5.4.3.
I add the following to my web.xml, as described in the Developer Guide, section "5.4.3.1. Configuring resource loading strategies":
<context-param>
<param-name>org.richfaces.resourceMapping.enabled</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.resourceMapping.packedStages</param-name>
<param-value>Production</param-value>
</context-param>
Now I see only 5 components that are not getting minified - but all the ajax functions in my application are no longer responsive (e.g. expanding/collapsing panels no longer happens). Also, I placed a a4j:log component at the foot of the page - that component is not displayed.
I guess the new minified versions of these resources cannot be found in the old, expected locations. I guess I therefore need to tell my application where to look - so, I go back to the content I skipped over earlier!
4. SECOND ATTEMPT
I revisit, with chagrin, the earlier section - 5.4.2.1 and the related sub-sections.
I add this to my web.xml:
<context-param>
<param-name>org.richfaces.resourceMapping.mappingFile</param-name>
<param-value>META-INF/custom-mapping.properties</param-value>
</context-param>
I take the "Packed.properties" file from /META-INF/richfaces/staticResourceMapping/ in the following JAR: richfaces-components-ui.jar. I add the contents to my custom-mapping.properties file. I create an ant task to ensure this gets added to the META-INF directory in my project war file:
<target name="-post-dist">
<jar destfile="${basedir}\dist\dashboard.war"
update="true">
<metainf dir="${basedir}\web\resources"
includes="custom-mapping.properties">
</metainf>
</jar>
</target>
Now, my web site runs OK again - ajax is responsive again - but I've reverted to YSlow telling me that "there are 30 components that can be minified" - for example:
http://localhost:8080/dashboard/faces/javax.faces.resource/richfaces.js
5. THIRD ATTEMPT
I try adding the following to web.xml:
<context-param>
<param-name>org.richfaces.resourceMapping.location</param-name>
<param-value>#{facesContext.externalContext.requestContextPath}/javax.faces.resource/#{resourceLocation}</param-value>
</context-param>
and, when that does not work (i.e. resources still not minified), also this:
<context-param>
<param-name>org.richfaces.resourceMapping.location</param-name>
<param-value>#{facesContext.externalContext.requestContextPath}/faces/javax.faces.resource/#{resourceLocation}</param-value>
</context-param>
I was not sure whether I should have used ".../javax.faces.resource/..." or ".../faces/javax.faces.resource/...". But neither appears to be what is needed.
Both of these result in the same YSlow feedback - 30 components not being minified.
6. CONCLUSION
So, I can get either:
- resources not minified, but site is working
or
- resources mostly minified, but site is not working
There's a note at the end of section 5.4.3.1 which states:
"Resource loading strategies are just special case of resource mapping, thus once you will provide custom resource mapping configuration or location, bundled default resources won't be referenced correctly.
"For using compressed/packed resources you will need to copy properties from one of files located in richfaces-components-ui.jar:/META-INF/richfaces/staticResourceMapping/."
I was not sure how to interpret this information. I guess the above walk-through shows you how close to (or far from) the correct answer I was.