Mohammad Norouzi wrote:
Hi all
I have written a component for JSF that needs a javascript file.
I packed the component inside a jar file and I want to put the js file inside that jar file so read that file as stream and my component use it.

in the renderer for my component I have to use ResponseWriter for building response, but as far as I know to attach another resource like js, I should use ResponseStream. but they dont work at a time with each other.

I have seen in myfaces components they are using some javascripts file in their api and use them, how can I use that feature?

There are two types of script: internal like
  <script ...>
    code here
  </script>
and external like:
  <script src="..."></script>

If you're just looking to output internal script then it's just a matter of being able to load the script file from within your renderer. I'm guessing that this is not what you want, however.

For external script the problem is that it is only valid for these script references to be in the HEAD section. Some browsers allow them in the BODY but that is not actually valid html. And there is no direct way for your component's renderer to output the script tag into the page head, because that is already written by the time your tag is processed.

The solution in Tomahawk is not pretty but it seems to be the only solution: the tomahawk ExtensionsFilter ensures the entire page is buffered as it is written. A renderer can call a special method to "register" text to be inserted into the head (and various other places like the body onload attribute). When the page is complete the ExtensionsFilter parses the page to find the head tag (and other bits of interest) and inserts any registered text (like script tags) at the appropriate places.

The script tag is rendered with a special prefix so that when the browser goes to load the script the request is sent to a url the ExtensionsFilter handles, and it then looks in the classpath for the data to be sent back.

There is also something that has been added to tomahawk recently called "Streaming Resources" (I think) that is an alternate to this approach but I don't know much about it. It does require the page to use a t:document tag I think.

This page has some more info:
  http://wiki.apache.org/myfaces/External_Resources

Regards,

Simon

Reply via email to