This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes Can Tag Handler class have complicated data type ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Can Tag Handler class have complicated data type ?" Watch "Can Tag Handler class have complicated data type ?" New topic
Author

Can Tag Handler class have complicated data type ?

Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
It seems the default attribute type for Tag Handler class is always "String". What if I need to pass a user-defined data type or just an arraylist ? For example, I want to do ---

In .jsp:

*********************************
<jsp:useBean id="My_Object" class="mypackage.data.MyObject" scope="request" />

<% Arryalist arraylist_1 = new ArrayList(); ...... %> //create an arraylist_1

<myprefix:mytag name="john" param1="arraylist_1" param2="My_Object" />
**********************************

In MyTag.java:

*****************
private String name;
private Arraylist param1;
private MyObject param2;

************
In other words, "param1" and "param2" are not String type. Is that OK ? From what I have tested it doesn't work at all. It seems it requires the type to be "String". That's not good. We often need to pass complicated things to a JSP. If a Tag Handler class only accepts String, can can I handle the situation that I need Arraylist or other user-defined data structure ?

Any thought ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Any thought ?


Yes. Take a look at what you wrote:



What is the value of param1? The string arraylist_1.

But that's not waht you want. You want the value of the scripting variable arraylist_1 to be passed.

So how would you differentiate the two?

Hint:

Let's say that you wanted to emit the value of arraylist_1 in a paragraph. Would you write is as:



?

No. How would you write it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
steve francisco
Ranch Hand

Joined: Jan 26, 2005
Posts: 46
Originally posted by Bear Bibeault:


What is the value of param1? The string arraylist_1.

But that's not waht you want. You want the value of the scripting variable arraylist_1 to be passed.

So how would you differentiate the two?

Hint:

Let's say that you wanted to emit the value of arraylist_1 in a paragraph. Would you write is as:



?

No. How would you write it?


If I am not mistakening you want Frank to "loop" through the ArrayList to retrieve the String values (correct me if I get you wrong). It sounds like it works, but I don't think it is a general way to do what he wants. What if the data type he wants to pass is more complex than an Array that you can loop through ?

I think the "general" way is to let the action class use "request.setAttribute" to pass that specific data type instance. And in the Tag Handler class it can use .getRequest to retrieve that instance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

(correct me if I get you wrong).


You get me wrong.

He has a simple syntax error and I am trying to get him to see it.

I don't think it is a general way to do what he wants.


Yes, he can do exactly what he wants. He just needs to get the syntax right.
[ April 10, 2005: Message edited by: Bear Bibeault ]
Dan Novik
Ranch Hand

Joined: Jan 26, 2005
Posts: 39
Use this:

<myprefix:mytag name="john" param1="<%=arraylist_1%>"

and describe a setter in your tag so:

public void setParam1(Object o)
{
...
}

it works
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Tag Handler class have complicated data type ?