aspose file tools*
The moose likes JSP and the fly likes Is it easy to create custom tags in JSP/JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Is it easy to create custom tags in JSP/JSTL" Watch "Is it easy to create custom tags in JSP/JSTL" New topic
Author

Is it easy to create custom tags in JSP/JSTL

Kenneth Gustafsson
Ranch Hand

Joined: Jan 10, 2008
Posts: 40
Hi,

I'm in the beginning of a project, no code has yet to be written. I want to reuse as much code as possible so I thought I'd write custom components which I then just use.

The components I'm thinking about are for various things. It could be a component which shows a static message if the user hasn't got JavaScript enabled, and a dynamically updated one if it's available. Other components could help with easy form creation and validation. Yet other components could handle nice tabular lists which you're able to update and re-sort using JavaScript.

Is it easy and flexible to do this using JSP or JSTL? And if it's easy and flexible using both ways, do you have any recommendation regarding if I should choose JSP or JSTL?

As a last note. I need to create custom components myself. :-) I can't imagine that all the custom things we'll use are available in any library. However I will use the help of jQuery to create the dynamic things.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
It's not really a question of JSP or JSTL. Just about all JSP pages will use JSTL tags. But JSTL tags are standardized; they aren't related to whatever tags you develop.

Since you mention lists, you may want to check out the DisplayTag tag library on SourceForge. It handles list display, paging, data export etc. nicely.


Ping & DNS - updated with new look and Ping home screen widget
Kenneth Gustafsson
Ranch Hand

Joined: Jan 10, 2008
Posts: 40
Ah, thanks for informing me! I was more thinking about using only the XML structure or the normal JSP structure.

Still the issue is code reuse and I'm wondering how hard it is for me to write my own tags/components. It's great that there are third party libraries with lots of tags, but I want to create some of my own.

For example, if I want to create a special kind of input text field which has some validation information as well:

<myinputtextfield id="textfield" param="name" desc="Input name:" " longdesc="Write your first name followed by your last name" mustmatchpattern="([a-z]{2,10}+\s){2}" onvalidationfailure="You must input your first name followed by your last name"/>

It will output a text field together with some JavaScript for validation as well as visual bling-bling. I might base it on jQuery, echo or something else.

Would that be possible? Would it be easy?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
A decent tutorial on writing tags libraries is at http://www.orionserver.com/docs/tutorials/taglibs/index.html
Kenneth Gustafsson
Ranch Hand

Joined: Jan 10, 2008
Posts: 40
Thank you! I'll be sure to read it. Just a quick question for you or someone else who is knowledgeable.

If there were lots custom ways needed to handle forms and other things and you wanted to reuse code to minimize bugs and development effort. Would you go for a JSP solution then, or would you use JSF, or maybe Wicket?
[ July 24, 2008: Message edited by: Kent Larsson ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
That's a loaded question, and you'll get lots of different opinions. Personally, I'd use a framework for anything but the simplest forms. My favorite at the moment is Stripes. But the answer depends very much on whom you ask :-)
Kenneth Gustafsson
Ranch Hand

Joined: Jan 10, 2008
Posts: 40
Thanks for answering me even though it was a loaded question! :-) Personally I lean mostly towards Wicket.

On a general basis, and as a follow up question, why would you use a framework instead of a plain Servlet+JSP architecture for anything but the simplest applications?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
Because a framework can do many things that otherwise one would have to code by hand: mapping of requests to server classes, binding of request parameters to Java objects, validation, form generation including repopulating of forms and display of error messages, ...

The reason I like Stripes in particular is that it keeps all the configuration in the Java class in the shape of annotations - no config files to maintain. It also emphasizes "convention over configuration", meaning that if HTML form fields and Java classes/methods/fields are named in certain standard ways, then one doesn't need to configure them at all - binding, mapping and validation are done automatically based on the names and IDs of things.
[ July 24, 2008: Message edited by: Ulf Dittmer ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is it easy to create custom tags in JSP/JSTL
 
Similar Threads
Java community took Java too far?
Tuscany SCA - newbie questions
web interface to search database
To author
This weeks giveaway