4 Replies Latest reply on Jan 28, 2010 4:07 AM by apanag

    Load Resources (JS & CSS) from a servlet

    apanag

      Hi,

       

      My question is how to load a JavaScript / CSS Resource file from a servlet, using the ajax4jsf API.

       

      My goal is to concatenate all the JavaScripts and all the CSS files to a single file, after the rendering of the pages

      I have added a Filter in my web.xml after the seam filter, which works fine.

       

      I cannot quite understand which classes from the ajax4jsf interface to use to load the resources as InputStreams.

       

      Here's my filters code:


      public class PackFilter implements Filter {

       

          public void doFilter(ServletRequest req, ServletResponse res,
                   FilterChain chain) throws IOException, ServletException {

       

              HttpServletRequest request = (HttpServletRequest) req;



               HttpServletResponse newResponse = new CharResponseWrapper(
                       (HttpServletResponse) res);

       

              chain.doFilter(req, newResponse);

       

              String text = newResponse.toString();

       

              if (text != null) {
                   res.getWriter().write(text);
               }


              String sourceURI = "org/richfaces/renderkit/html/scripts/skinning.js";
               //String sourceURIFull =  "/my-web/a4j/g/3_3_2.SR1org/richfaces/renderkit/html/scripts/skinning.js";
                      
               LoadResourceRendererBase rb = new LoadResourceRendererBase ();
              
               InternetResource internetResource;

              //unsuccessful efforts
               //internetResource = rb.getResource(sourceURIFull);       
               //internetResource = new org.ajax4jsf.javascript.AjaxScript();

              //InputStream inputStream
               ClientScript clientScript = new org.ajax4jsf.javascript.AjaxScript();

             
               InternetResourceBuilder resourceBuilder = InternetResourceBuilder.getInstance();
              

      Thanks,

       

      Andreas

        • 1. Re: Load Resources (JS & CSS) from a servlet
          apanag

          Anyone?

           

          Do I do something totally wrong or something?

           

          I've seen a4j:loadScript and I do not see how it can be of help to me.

           

          ok with LoadStyleStrategy or LoadScriptStrategy I avoid loading the resources.

           

          <param-name>org.richfaces.LoadStyleStrategy</param-name>

          <param-value>NONE</param-value>

           

          but how do I access the resource?

           

          thanks


          • 2. Re: Load Resources (JS & CSS) from a servlet
            apanag

            RichFaces offers the Scripts and Styles Strategy which is unfortunately not sufficient for my needs.

            Why would I want to include the whole ui-library when I am only using a component at one page and many ui components at another?

            Speed is very important, and maybe the user just reach only to the first page. Plus I want to keep the number of request to a minimum.

             

            With the org.richfaces.LoadScriptStrategy set to ALL , my custom RF Components' JS files don't get loaded.

            Is there any solution for the latter?

            Something that I am missing?

             

            My goal is write a filter / extend the RF functionallity in a way that:

             

            1. it parses the whole page at the end of the rendering gathering the JS files

            2. It combines (concatenates) all the JS files, creates a pack.js and caches it.

             

            At the next request of this page, the pack.js is immediately available.

            (same for the css)

             

            Any ideas why I should not do that, or other solutions?

            • 3. Re: Load Resources (JS & CSS) from a servlet
              nbelaevski
              1 of 1 people found this helpful
              • 4. Re: Load Resources (JS & CSS) from a servlet
                apanag

                Thanks a lot Nick,

                 

                that solved most of my problems.

                 

                It^s kind of hard to spot the document.

                Yes, now I know that there is stuff in the Articles, but when I search in the forum for that or google, I found null.

                It's really a pitty, that even though so many cool features and they are hard to find.