Google
 
Web unafbapune.blogspot.com

Saturday, February 25, 2006

 

Alternative to implementing js as jsp

Here is an idea that is not as simple as just directly implementing js as jsp, but is probably more performant and maintainable: for each js, we also have a corresponding jsp. For example

Foo.js
Foo.js.jsp

Within Foo.js, it's pure static javascript, but optionally with "hook"'s that the Foo.js.jsp can fill in during runtime on the enclosing page. For example:

Foo.js
------
// Constructor of the Javascript Foo class.
function Foo() {}
...
// Anytime it needs to access a dynamic property that needs
// to be determined by the Java App, or tag lib, or whatever,
// use a class level variable.
// For example, a i18n message to be displayed.
alert(Foo.FOO_MESSAGE);
...

Foo.js.jsp
----------
// Foo.js.jsp is included in the main page where the reference to Foo.js is made.
// If there is no dynamic content, Foo.js.jsp just evaluates to do nothing.
Foo.FOO_MESSAGE = "<c:out name="${fooMessage}" />"
This way, all the dynamic content are included as Javascript class level variables in the main page, and the static Foo.js just refers to these variables.

Now all the *.js can be pushed to the Web server front end (eg Apache) - solves the performance issue. All the dynamic content is maintained in one single place: the respective jsp connected via a javascript class - solves the maintenance nightmare issue.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?