Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Portlets and External Javascript Files

 
Troy Ballantine
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic