Thursday, April 07, 2005

Jsp page architecture

What architecture should I use when using Java Server Pages? Sun leaves us guessing. There is "Model 1" — which loosely translated means the JSP page contains everything in a hodge-podge. And there is "model 2" — which, again roughly translated, means that you do your homework first and let the JSP do just the display part of the deal. That still leaves us with a lot of questions.
Here's my answer (YMMV – Your Milage May Vary):

  • Your URI translates into a servlet (which might be a Struts Action, or equivalent). The servelet (or Action) talks to the "back-end" — hopefully directly or indirectly to your Domain Object Model (DOM).

  • The Servlet/Action does all neccessary preparation to display the data / entry form. That includes converting raw data to display format (applying I18N / user preferences etc.).

  • The servelet forwards to the JSP.

  • The JSP starts with the obligatory jsp stuff (page includes, taglib includes and the like.)

  • Then there is an absolutely minimal scriptlet to do stuff that absolutely must be done in the page (maybe a log trace statement or something)

  • From here on, only tags – HTML and JSP (including custom tags) are permitted


You may ask – Absoultely no scriptlets in the HTML? Correct. None whatsoever. Iteration? Struts/JSTL iteration maybe, but definately no <% for (int n = 0; n < length; n++) %>. Never, ever. Just say no. Do all your preparations beforehand and let the JSP render it.

Advantage? Your pages become simpler and easier to work with. Pages that mix Java scriptlets and HTML are understandable by neither your GUI folks, nor your Java developers. Whoever tries to make changes will trample on the other's domain. It will end in tears, believe me. Don't go there.

[Last minute addendum: You might ponder over tables (the tabular data sort, of course). If rendering tables using scriptlets is out – and it is – it doesn't mean you just do the same iteration using Struts/Jstl. If you haven't done already, go to http://displaytag.sourceforge.net/ immediately. Disclaimer: I don't use displaytag myself; I have my own library that contains a similar (in concept, but not implementation) tag.]
Category: ,

0 Comments:

Post a Comment

<< Home