2 Replies Latest reply on Apr 17, 2008 4:43 AM by mars1412

    loadScript question

    mars1412 Apprentice

      When should I use resource:// in the source attribut of loadScript and when not? What is the difference?

      From what I read in the docs:

      I guess it's like this:

      • use src="/js/myjavascript.js" for my own java script files (if located in a direcotry called js in my webroot)
      • use src="resource:///SomeOtherJs.js" for java script files included in the richfaces jars (e.g. prototype, ..)
        is that correct?

        • 1. Re: loadScript question
          Sergey Smirnov Master

          Your first statement is correct. The description of the second one sounds too limited.

          Why only in the richfaces jars? Anyone. However, if I say "from any jar", it will be a limited description as well.

          If you have /js/myjavascript.js in your webroot,
          src="resource:///js/myjavascript.js" will work as well as src="/js/myjavascript.js".
          The webroot is on of the place where resource:// takes resource from.

          You can use "resource://" not only with a4j:loadScript or a4j:loadStyle. Any JSF components that have an attribute with path to resource are suitable.

          For example:
          <h:graphicImage value="resource:///images/foo.gif" /> takes the picture as well as <h:graphicImage value="/images/foo.gif" />

          Sure, it is not the miracle. However, you can NOT say:
          <h:graphicImage value="/WEB-INF/secretStore/images/bar.gif" />

          <h:graphicImage value="resource:///WEB-INF/secretStore/images/bar.gif" /> will work

          resource:// locates resources in the classpath

          So, if you want to store your pictures in the jar file or/and in the WEB-INF/classes, you can do it. Just use resource:// to access to them.

          What if you want to create image dynamically from the java class?
          resource:// allows to have an access to it.

          For example, if your class com.mycompany.graphic.UniChart.java implements InternetResource interface, you can say:

          <h:graphicImage value="resource://com.mycompany.graphic.UniChart" /> to draw something excited dynamically and put down on the page.

          • 2. Re: loadScript question
            mars1412 Apprentice

            wow! - thanks for the detailed answer.
            maybe this should go into the docs.

            I now understand what went wrong in my case:

            I tried to use http://tinymce.moxiecode.com/ in my project.
            I tried to load the tinyMCE javascript like this:

            <a:loadScript src="resource:///tiny_mce/tiny_mce.js" />

            which does not work as expected.
            problem is, that the tiny_mce.js tries to get the baseURL of the script and then load some additional java-script resources (plugins, langpack, ..)
            when I use resource, the base url will be smth. like this:

            and thus, tinyMCE tries to load additional resources like this:

            which will not work and gives this exception on the webservers console:
            10:33:04,553 ERROR [[default]] Servlet.service() for servlet default threw exception
            org.ajax4jsf.resource.ResourceNotFoundException: Resource not registered : tiny_mce/langs/de.js
             at org.ajax4jsf.resource.ResourceBuilderImpl.getResource(ResourceBuilderImpl.java:389)
             at org.ajax4jsf.resource.ResourceBuilderImpl.getResourceForKey(ResourceBuilderImpl.java:333)
             at org.ajax4jsf.resource.InternetResourceService.serviceResource(InternetResourceService.java:152)
             at org.ajax4jsf.resource.InternetResourceService.serviceResource(InternetResourceService.java:141)
             at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:481)
             at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
             at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)

            when only looking at the exception text, it was no clear to me why this file is not found, as the path semms to be ok: tiny_mce/langs/de.js

            using this to load the script:
            <a:loadScript src="/tiny_mce/tiny_mce.js" />

            solves the problem, because now tinyMCE will find the correct baseURL:

            and request the correct resource: