Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Helper Objects: Custom tags vs. JavaBeans

 
John Pritchard
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a fundamental understanding of the implementation of custom tags and JavaBeans as helper or utility objects. Do any of you guys have a handy rule of thumb about when one is more appropriate to use than the other?
It would seem from a pure project staffing perspective that I can keep my JSPs simple (and staffed with UI type staff) and implement the majority of my logic as JavaBeans done but developers...
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use JavaBeans to keep track of state. You can use the JSTL Expression Language (or the Struts expression language, if that's more your cup of tea) to extract this state and display it.
Use tags to perform any kind of processing, such as formatting, iteration, and conditionals. Of course you'd use prefab taglibs where possible e.g. JSTL or the Struts taglibs.
I'm not convinced that the complexity of taglibs is really a lot higher than that of JavaBeans. Using JavaBeans for processing means using scriptlets, and that can certainly make an unmaintainable mess of your JSP!
UI type staff is best kept away from any Java code. With the JSTL or the Struts logic tags, they can use tags to express simple logic. Anything more complicated is for Java developers.
- Peter
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I use JavaBeans to hold entities like a person, address, item etc. The Bean knows how to store, manipulate this data - it does not know anything about HTML or how to present this data in any way (the model in MVC terms).

Tags act as a proxy between some entity which holds data and how to present it to the user.
I use tags to keep my JSP clean from Java code (scriptlets), create the presentation layer with hopefully clean and short tag code, access beans (the model data) from tags.

Regards,
Bernd
SCJP
 
John Pritchard
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter/Bernd-
Thanks. That clears things up nicely. A similar paragraph in Professional JSP or the Manning book would have been uselful.
regards,
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Pritchard:
Peter/Bernd-
Thanks. That clears things up nicely. A similar paragraph in Professional JSP or the Manning book would have been uselful.
regards,

Indeed it would have - which is why I devoted a fair chunk of a chapter to it in Professional JSP Tag Libraries. It can be found in Chapter 8 (Tag Patterns) (available to download for free in PDF) if you're interested in this topic. I also present a some of the behind the scenes mechanics as justifications, and a comparison of how beans and tags are used on the page.
All the best
Simon
 
John Pritchard
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon-
Thanks for the reply. I will make a point to add your book to my "Need 2 Read" list.
I also know it's much easier to edit than write
You have my continued thanks for producing literature for us.
Cheers,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic