Import JavaScript into JSP tags

I have a .tag file that requires a JavaScript library (as in a .js file).

Currently, I just remember that I imported the .js file into every JSP that uses the tag, but this is a bit cumbersome and error prone.

Is there a way to import .js inside a JSP tag?

(for caching reasons, I would like a .js script to be imported)

+7
java javascript jsp jsp-tags
source share
2 answers

There is no reason why you cannot have a script tag in the body, although it is preferable to be in the head for it. Just release the script tag before you fix the markup of your tag. The only thing to consider is not that you do not want to include the script more than once if you use the jsp shortcut on the page more than once. The way to solve this is to remember that you have already included the script by adding an attribute to the request object.

+5
source share

Except for just including js on every page automatically, I don't think so. This is really not what the tags are for.

Not knowing what your tag is doing (presumably its conclusion from something in a section of the body), there is no way that it can place a declaration there on time.

One solution that might (in my head) be to have an inclusion that verbatim copies what you have in your head after the place in your head to import the tags right where you want to use the tag. This is really not what you would like to do. You will have to have several header files to import, depending on the content and where you want to use the tag. A nightmare for service. Just a bad idea. Any solution I can think of will require more work than just adding to the declaration manually.

I think you're out of luck and stuck in manual entry.

edit: just import it into each page. It will be cached and then this problem will disappear.

+2
source share

All Articles