tags:

views:

94

answers:

1

I am currently writing a template for OpenCms, a Java based CMS with a very good taglib-support and a powerfull API.

Anyway, when I look at tutorials or the source, its about 50-50 that taglibs or Javaclasses are used to generate (html)-output.

I try to keep my JSPs clean, meaning not to much scriplet code (instead using JSTL and so on). At some point I could need more control over the code, thats were the Java-classes come in and here I dont know how far I should go (I personaly dont like the idea of generating HTML in a class).

Here are some pros & cons I have though of:

  1. With taglibs, I dont have to restart the servlet container each time. I just make my changes and see the outcome
  2. A taglib JSP is easier to maintain, especially for webdesigner because of the "same" syntax
  3. A Javaclass provides full/more IDE support (type/syntax check)
  4. With more complex requirements the Javaclass-code is cleaner (compared to putting variables to the pagecontext in JSPs)
  5. EL (expression language) add more power to the JSP/taglib but thats another syntax again and needs to be learned

Are there any best-practises on how to approach this? Since there are other collegues, that probably have to maintain this in the future, I am looking for a clean/maintainable solution - You probaly all know the situation when you looked at somebodies code and though "what the hell was he/seh thinking" ;)

A: 

I have kept this pretty simple in the past.

Use taglibs when the purpose is presentation. Say you have a block of display code that you are going to reuse in a bunch of pages, in the case of StackOverflow, let's say the arrow code. Since that should just be displaying data, it shouldn't need and Java code, and I'd use a taglib.

Use Java code when you need to convolve data. If you have to look in a resource bundle or apply some non-generic formatting to something, I would use Java code, although I prefer JSTL functions to tags for this.

Don't use either to access databases.

stevedbrown