Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Organizing/Modularizing Javascript in large JSP file  RSS feed

 
Maurice Lowenthal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have rather large jsp file

This file is big because I use Ajax and this page is a WEB2.0 type application.

There is alot of Javascript and within the JSP I sometimes use JSP scriptlets which reference
some java libraries that I import

For example


function getPortfolioGroups() {
var jsonRequest = {requestType:"<%=CoachConstants.RESPONSE_PORTFOLIO_GROUPS%>"};
coachAjaxCall(jsonRequest);


I can't save the javascript code as separate js files because there are scriptlets in the code.

I'd like to refactor this code and make it more modular and take it out of the jsp file


Does anyone have any suggestions?

Is there a standard approach to organizing a file structure for this?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Putting aside the fact that scriptlets are passé in modern JSPs (since you'd be having the same issue with modern JSTL/EL), the same types of modularization used in Java can be used in JavaScript.

But as you pointed out, this creates an issue because external script files aren't passed through the JSP translator. Generally, such close coupling between the server and the client is frowned upon, so it should be kept to the minimum. But sharing such things as constants between the client and the server is a useful way to make sure that they don't get out of sync.

I usually handle this by not sprinkling any server-side constructs throughout the JavaScript code. That way, the script is free from the need to be translated, and I can just go ahead and organize the script into external script files in a way that make the most sense for the application.

To handle the dynamic values coming from the server, I declare JavaScript constructs in one place in the head of the page, and all the other scripts refer to these constructs. In fact, I usually define a custom tag whose job it is to create the JavaScript constructs.

This segregates the server<->client interaction to one place and completely frees all the rest of the script from reliance on the server and JSP translator.
 
Maurice Lowenthal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear,

That makes sense
 
Ketan Parikh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maurice,

You may consider Jsp fragments (.jspf). I used to modularize my larger Jsp file into set of .jspf files. As your code is divided into multiple fragments, it is easy to read and update it.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ketan Parikh wrote:You may consider Jsp fragments (.jspf). I used to modularize my larger Jsp file into set of .jspf files. As your code is divided into multiple fragments, it is easy to read and update it.

JavaScript should be included via script tags, not JSP includes. Otherwise, all the benefits of browser caching are lost.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!