Custom taglibs: do I need one class per tag?

J

james

I've been crash-course learning java, jsp, servlets, struts, etc
(Coming over from php & asp, and I love it!). I'm trying to figure out
proper style in the process.

I've been creating a custom taglib for my app to retrieve data so JSP
pages can render them. Do I have to create a separate class for each
custom tag? For servlets/struts, I can use a DispatchAction class to
feed multiple actions into the same class, using a parameter to tell
which method I want to invoke. Is there anything similar with custom
tags? I would like to have the following.

<myApp:getItems parameter="allItems"/>
<myApp:getItems parameter="oldItems"/>

And they would correspond to the following methods, respectively:

class GetItemsTag extends TagSupport {
public void getAllItems() { ... }
public void getOldItems() { ... }
}

I can do this with two separate tag classes, or by using a big "if (
parameter.equals(...) ) {" for each action, but this would seem to make
things just one bit simpler. Is there any such shortcut? Am I trying
to abuse the model / view paradigm?

Also, as far as I can see, in a typical struts app I have a couple of
possibilities for rendering pages:
1) I can goto page.jsp and use custom tags to grab all the data
necessary, then post any forms to an action...
2) or I can always go to page.do, have that action setup all the
necessary data in request vars, and then forward to the proper
rendering JSP page.

Are both of these styles acceptable? Is either blatantly wrong, or
does it come down to preference and pros/cons of each style? What are
your preferences?

Thanks for all feedback, I love java after dealing with ASP / PHP for
so long.
James
 
V

Venkatesh

As far as I know, doStartTag is the only method that is always called
for each tag.

The jsp tags framework doesn't provide implicit support for different
method invocations based on some attributes.

-Venkatesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top