1 2 Previous Next 23 Replies Latest reply on Mar 1, 2012 12:08 PM by Josh Schneider

    Problem Getting Started with Resource Loading in RF4.1

    undermanager Newbie

      This 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.

        • 1. Re: Problem Getting Started with Resource Loading in RF4.1
          undermanager Newbie

          I tried the work-around mentioned in this JIRA ticket - i.e. I added the following to the top of my site's template page:

           

              <a4j:jsFunction name="_nothing"/>

           

          In my case, this meant putting it immediately following the <h:head> tag.

           

          This is discussed in more detail in this thread.

           

          The result:  RichFaces resources are now being packed/minified.  This was reflected in an improved showing in YSlow.  Now, the only scripts not getting packed are mine.  All I needed in my web.xml to achieve this was the following - which is what I originally expected to work:

           

          <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>

           

          Next:  I tried to add the "org.richfaces.resourceMapping.mappingFile" and "org.richfaces.resourceMapping.location" parameters, but I'm still not getting that right - I reverted to nothing getting packed.  I will have to revisit this piece.

           

          But the workaround got me 85% of what I wanted.

          • 2. Re: Problem Getting Started with Resource Loading in RF4.1
            Lukáš Fryč Master

            Hi undermanager,

             

            Just few only points

             

            • workaround mentioned earlier (RF-11739) won't be necessary in 4.2 anymore
            • only one single configuration should be necessary to get resources packed/minified: org.richfaces.resourceMapping.enabled=true
              • note that ResourceServlet needs to be registered (which applies only in Servlet environments lower than 3.0)
              • if it doesn't work, it's an issue (either environment-specific or implementation one) and should be tracked in issue tracker!

             

             

            ========

             

             

            Now several observations I have made when I was reading through your great elaboration, and what should help me to improve docs:

             

            1. resource loading strategy is your target
              • you want to have packed/minified resources
              • but docs are covering resource mapping first, which is rather confusing
            2. when resource loading doesn't work, it is typically because either...
              1. resources aren't loaded at all (aren't served properly, 500 internal server error, 404 not found, etc.)
              2. resources are loaded, but in wrong order (then JavaScript errors are expected)
                • both issues can be verified by checking resources loaded by given page using developer tools (Firebug / Chrome Dev Tools (F12) / ....)
                • using Console tab (visible resource loading errors or JavaScript errors)
                • and Resources/Network tab (resource loading errors)
            • 3. Re: Problem Getting Started with Resource Loading in RF4.1
              Brendan Healey Master

              Are you using prefix or suffix mapping (faces/* or *.xhtml)? I couldn't make it work with suffix mapping. Also be aware that

              you need this (note the sequence):

               

              <h:outputScript name="jsf.js" library="javax.faces"/>

              <h:outputScript name="jquery.js"/>

               

              Also don't initially use a custom skin, if you want to make life easy!

               

              Brendan.

              • 4. Re: Problem Getting Started with Resource Loading in RF4.1
                Thomas Asel Newbie

                I can confirm the issue reported from undermanager. First of all I agree that documentation is a bit unclear and should be improved.

                 

                I got my application working by following the steps described above, but in the end resources wont be packed by RF.

                 

                These are my observations:

                 

                1. A request is made for packed.css and packed.js when the follwoing context-params are used. However, Neither packed.css nor packed.js can be found (HTTP 404)

                <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>All</param-value>

                </context-param>

                2. Adding the following context aparameters and a copy of packed.properties to MET-INF results in requests to non packed resources (/rfRes/extendedDataTable.ecss.xhtml?db=eAE78Y1hMgAGmQJS&ln=org.richfaces)

                <context-param>

                    <param-name>org.richfaces.resourceMapping.mappingFile</param-name>

                    <param-value>META-INF/custom-mapping.properties</param-value>

                </context-param>   

                 

                <context-param>

                    <param-name>org.richfaces.resourceMapping.location</param-name>

                    <param-value>#{facesContext.externalContext.requestContextPath}/javax.faces.resource/#{resourceLocation}</param-value>

                </context-param>  

                 

                So question is how to get resource packing to work in 4.1 ?

                • 5. Re: Problem Getting Started with Resource Loading in RF4.1
                  Lukáš Fryč Master

                  Thomas, to analyse your problem closer, I would need:

                   

                  1. what URL is generated into your page (the one which returns error 404)
                  2. configuration of ResourceServlet in web.xml

                   

                  This issue should not depend on FacesServlet configuration, but rather ResourceServlet configuration.

                   

                  ResourceServlet is automatically registered on servlet-mapping /org.richfaces.resources/*

                  URLs should look then like {contextPath}/org.richfaces.resources/..../packed.js

                  • 6. Re: Problem Getting Started with Resource Loading in RF4.1
                    Lukáš Fryč Master

                    Thomas, I have found issue what you have with custom resource mapping.

                     

                    <context-param>
                        <param-name>org.richfaces.resourceMapping.location</param-name>
                        <param-value>#{facesContext.externalContext.requestContextPath}/org.richfaces.resources/javax.faces.resource/org.richfaces.staticResource/#{a4j.version.implementationVersion}/%Features%/#{resourceLocation}</param-value>
                    </context-param> 
                    

                     

                    But that is not convenient, since you need to place your resources to adequate folder!

                     

                    I will fix that in RichFaces 4.2.

                    • 7. Re: Problem Getting Started with Resource Loading in RF4.1
                      Lukáš Fryč Master

                      I have created issue to track custom resource mapping problem: https://issues.jboss.org/browse/RF-11909

                      • 8. Re: Problem Getting Started with Resource Loading in RF4.1
                        Thomas Asel Newbie

                        Lukáš, thank you for the quick reply.

                         

                        Is the workaround mentioned above valid for RF 4.1?

                         

                        Changing

                        <context-param>

                            <param-name>org.richfaces.resourceMapping.location</param-name>

                            <param-value>#{facesContext.externalContext.requestContextPath}/javax.faces.resource/#{resourceLocation}</param-value>

                        </context-param>

                        to

                        <context-param>

                            <param-name>org.richfaces.resourceMapping.location</param-name>

                            <param-value>#{facesContext.externalContext.requestContextPath}/org.richfaces.resources/javax.faces.resource/org.richfaces.staticResource/#{a4j.version.implementationVersion}/%Features%/#{resourceLocation}</param-value>

                        </context-param>

                        does not seem to have any effect. This is what the requests look like:

                        http://localhost:8080/MyApp/javax.faces.resource/jquery.js.jsf

                         

                        There is no explicit configuration for the ResourceServlet as my application is running in a Servlet 3.0 environment.

                        • 9. Re: Problem Getting Started with Resource Loading in RF4.1
                          Josh Schneider Newbie

                          Should this

                          <context-param>

                              <param-name>org.richfaces.resourceMapping.location</param-name>

                               <param-value>#{facesContext.externalContext.requestContextPath}/org.richfaces.resources/javax.faces.resource/org.richfaces.staticResource/#{a4j.version.implementationVersion}/%Features%/#{resourceLocation}</param-value>

                          </context-param>

                           

                          actually be written something like

                          <context-param>

                              <param-name>org.richfaces.resourceMapping.location</param-name>

                               <param-value>#{facesContext.externalContext.requestContextPath}/org.richfaces.resources/javax.faces.resource/org.richfaces.staticResource/4.1.0.Final/PackedCompressed/#{resourceLocation}</param-value>

                          </context-param>

                          or should those EL path variables resolve correctly depending on what's in your web.xml?

                           

                          Actually, I'm getting entirely confused about general resource mapping versus or in combination with static resource mapping. My goals are to try overriding packed.js with a cut-down version (minus jQuery), so I can load a more recent jQuery core, and then initialize with compression and packing for production (or all stages, for testing purposes). Also, I think I need to combine this with JSF resource loading to handle jQuery core, since only pages with rich: or a4j: will make RF-loading-strategy resources load, ignoring pages where I just have plain old jQuery or jQuery UI going on. I hope the documentation is improved and that RF 4.2 will handle some of the issues I'm seeing with the 4.1 methods. Thanks.

                          • 10. Re: Problem Getting Started with Resource Loading in RF4.1
                            Lukáš Fryč Master

                            Hi guys,

                             

                            just note that resource loading was significantly re-work in 4.2.0.CR1 -

                            resource mapping and resource optimization were separated.

                             

                            You can give it a try using instructions in following blog:

                            http://rik-ansikter.blogspot.com/2012/02/optimizing-resource-loading-with.html

                             

                            details of what has been changed are in the release blog:

                            http://blog.bleathem.ca/2012/02/richfaces-420cr1-release-announcement.html

                            • 11. Re: Problem Getting Started with Resource Loading in RF4.1
                              Brendan Healey Master

                              I've got to say that the forthcoming blog post can't come soon enough. The resource handling functionality is a big

                              bonus and well done to you all for putting in all the effort to make it happen. The problem is that pre resource

                              mapping I used to have a .properties file that was my customised skin. This could include a reference like

                              baseSkin=blueSky, then you'd reference this from web.xml  with the org.richfaces.skin parameter. I was overriding

                              three properties in my "custom skin". I thought it was a really powerful feature.

                               

                              This approach doesn't work with resource mapping enabled so you have to use the maven-richfaces-resource-plugin -

                              I think. I'm now at the point where I've no idea exactly what maven config I'm supposed to be using. Should I have

                              anything in settings.xml, or is that now out of date? Do I need to also include the cdk in my maven config, and if

                              so how? One of the biggest problems is that every maven related post seems to link to several other maven related

                              posts, and you end up going round in circles trying to make a note of which snippet of information was the most

                              recent. As it stands I don't know that the status quo is.

                               

                              I may be wrong, but although a great concept I'm pretty sure the maven implementation, or the way it's able to be

                              used, has gone horribly wrong. Take a look at this, it's like a bad case of vertical double vision!

                               

                              https://community.jboss.org/wiki/MavenGettingStarted-Developers

                               

                              <repositories>
                                  <repository>
                                    <id>jboss-public-repository-group</id>
                                    <name>JBoss Public Repository Group</name>
                                    <url>http://repository.jboss.org/nexus/content/groups/public/</url>
                                    <layout>default</layout>
                                    <releases>
                                      <enabled>true</enabled>
                                      <updatePolicy>never</updatePolicy>
                                    </releases>
                                    <snapshots>
                                      <enabled>true</enabled>
                                      <updatePolicy>never</updatePolicy>
                                    </snapshots>
                                  </repository>
                                </repositories>
                                <pluginRepositories>
                                  <pluginRepository>
                                    <id>jboss-public-repository-group</id>
                                    <name>JBoss Public Repository Group</name>
                                    <url>http://repository.jboss.org/nexus/content/groups/public/</url>
                                    <releases>
                                      <enabled>true</enabled>
                                    </releases>
                                    <snapshots>
                                      <enabled>true</enabled>
                                    </snapshots>
                                  </pluginRepository>
                                </pluginRepositories>

                              • 12. Re: Problem Getting Started with Resource Loading in RF4.1
                                Josh Schneider Newbie

                                Thanks for all your work on resource loading updates and documentation. One thing that I've been trying to figure out for a long time (Brian has anticipated this, and knows it's an issue) is separating out the third-party script libraries from RF-specific script. May be pie-in-the-sky anytime soon, but if you could rework your scripts so that jQuery core and jQuery UI were entirely modular, they could be easily overridden. It would be up to the developer to determine whether that would break anything, but I think a cleaner approach. Of course, that would also mean not including them in any packed.js, which would necessitate the use of other, downstream optimization options, but still, that would be my vote. Thanks again.

                                • 13. Re: Problem Getting Started with Resource Loading in RF4.1
                                  Lukáš Fryč Master

                                  Hey Josh, I know we have already discussed this, just for reference:

                                   

                                  Resource Mapping actually enables you to replace any JSF resource (any JS/CSS provided by any JSF component library).

                                   

                                  Does it cover your modularity concerns?

                                  • 14. Re: Problem Getting Started with Resource Loading in RF4.1
                                    Lukáš Fryč Master

                                    Hey Brendan,

                                     

                                    I would start from scratch - see the reference how to start with RichFaces Maven-based app [1]

                                     

                                    1. start with simpleapp [1]
                                    2. configure pom.xml to generate optimized resources (compressed and packed) with one of build-in skins (e.g. blueSky) [2]
                                    3. configure project to use newly generated resources (this is only un-clear step afaik)
                                      1. you need to configure mappingFile to point to configuration file (list of resources to map to their packed versions)
                                      2. this file is generated by the plugin (staticResourceMappingFile)
                                    4. start the app and verify that optimized resources are served (check my blog to see what's the difference [3])

                                     

                                    Note: be sure to use 4.2.0.CR1 (or Final version once available)

                                    Note: if you run into issues with configuring plugin (the docs are for 4.1), check what RF do in packaging process [4]

                                     

                                    Now for your custom skin:

                                    1. add custom skin properties file to the project
                                    2. reconfigure maven plugin (pom.xml) to use your skin
                                    3. reconfigure web.xml to let RichFaces grab this plugin

                                     

                                     

                                    Btw if you would run into issues, don't hesitate to contact us on IRC #richfaces at irc.freenode.net.

                                     

                                     

                                    [1] http://docs.jboss.org/richfaces/latest_4_X/Developer_Guide/en-US/html/chap-Developer_Guide-Getting_started_with_RichFaces.html#sect-Developer_Guide-Getting_started_with_RichFaces-Creating_a_project_with_Maven

                                    [2] http://docs.jboss.org/richfaces/latest_4_X/maven-richfaces-resources-plugin/usage.html

                                    [3] http://rik-ansikter.blogspot.com/2012/02/optimizing-resource-loading-with.html

                                    [4] https://github.com/richfaces/components/blob/4.2.0.20120215-Final/dist/static-resources/pom.xml#L271

                                    1 2 Previous Next