File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Portals and Portlets and the fly likes Portlets and External Javascript Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Portals and Portlets
Bookmark "Portlets and External Javascript Files" Watch "Portlets and External Javascript Files" New topic

Portlets and External Javascript Files

Troy Ballantine

Joined: Oct 31, 2008
Posts: 1
I wanted to post this because I searched for an answer to this over 2 years ago and didn't find one. I just search today and didn't either. I found two old postings on the JavaRanch from back in 2007 and neither of them had what I would call an elegant solution. I think I finally have a pseudo-elegant solution and I want to see if anyone can poke holes in it.

The issue is that I want to move javascript out of my jsp into a js file in a portlet based application. Since I am dealing with portlets then I still needed to be able to portlet namespsace my javascript methods and variables so that I would not step on the toes of other portlets. Given this issue we had a lot of duplicate javascript code. The answer was staring me in the face because we had already instituted something that would work for our company javascript library that was based on jQuery. I templated it today and call it AJL (Application Javascript Library). The developers will be able to copy the AJL code template you will see below into their portlet application and start coding.

Here is the template:

Here is my test html. I did not do portlet code because we are using this "design" already in production so that is tested out.

Once I have it out in a js file I can also minify it which is something I could not do if I went the include jsp route. This solution also addresses an issue a developer is having here in that a compiled JSP exceeded some size limit. I won't go into the whole design of the page since it could be argued it is too big but being able to remove the JS will help solve that for him. If we did an include JSP mechanism it still would have been part of the compiled page.

Please let me know what you think.
Jot Haa

Joined: Aug 23, 2012
Posts: 1
Troy Ballantine wrote:Please let me know what you think.

This is a nice approach!

I've been using it in a Portlet and i want to make the <portlet:namespace /> also accessible to my JavaScripts, to safely manipulate elements with jQuery.

at this point, the namespace is fine in <portlet:namespace />_ajl.

But when i try to register an event handler:

on keypress in <portlet:namespace />_ajl.updateFormAction the namespace is undefined?!

This is my ajl.js (stripped down):
I agree. Here's the link:
subject: Portlets and External Javascript Files
It's not a secret anymore!