File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Tag File Variable Synchronisation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Tag File Variable Synchronisation" Watch "Tag File Variable Synchronisation" New topic
Author

Tag File Variable Synchronisation

Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
I'd like to bring up something that's been bothering me ever since the following topic came up in the SCWCD forum:

@variable declare attribute

It's about the variable directive used in tag files to declare scoped attributes to be synchronised between the JSP Context Wrapper (the tag file's context) and the Invoking JSP Context (the calling page's context).

It takes all the same attributes as the <variable> element in the TLD - i.e. all the same attributes as for synchronisation which occurs between classic tags and JSP scripting variables. It also has the "alias" attribute for use alongside "name-from-attribute".

But as far as I can see, the "variable-class" and "declare" attributes are useless because we're synchronising between two PageContext page-scoped attributes, and not storing anything in a specific Java variable. Since all scoped attributes are always stored and retrieved as Objects, with EL doing the casting to a required type on retrieval implicitly (and any JSP scripting elements have to do casting manually anyway), "variable-class" seems unnecessary. Also, since the scoped attribute containers store attribute names against values, there seems to be no point in having a "declare" variable either, as entries in a Map-style implementation don't need to be declared in the Map before they can be set.

I've tested my theory with Tomcat: I used "java.util.List" as the value for "variable-class" and then managed to store all kinds of other objects (Numbers, Strings, even an Exception) without problem in the variable. I also set "declare" to true and false, and compared the translated JSP results for each: and they were exactly the same. Plus the source code for org.apache.jasper.runtime.JspContextWrapper shows nothing to contradict my previous statements (if anything it supports it).

So it appears Tomcat completely ignores these attributes, and I just wondered if anyone actually uses them or knows of a reason why they exist for tag files... otherwise I can only assume they're a left-over relic from classic tags, perhaps with a view to some kind of future implementation?

Thoughts welcomed...


Addendum: I'd like to add that all tutorials (even those from Sun) always include reference to these attributes, but never, ever use them in any examples!
[ July 25, 2006: Message edited by: Charles Lyons ]

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Hi Charles.

(I may be out of subject)
If you do not specify the variable-class, it defaults to java.lang.String.
How can you set the variable to another class then, without setting the variable-class ?

The following uses a bean called beans.UserInfo



This will produce an error, but setting variable-class to beans.UserInfo won't.

Setting "declare" to false will also produce an exception in the calling JSP:
"userInfo cannot be resolved".
Setting "declare" to true add this line to the calling JSP : beans.UserInfo userInfo = null;

Maybe you have used the NESTED scope, and used the variable in the tag's body, which could explain why you don't get errors. Could you please confirm by setting the scope to AT_END (or even AT_BEGIN).


[My Blog]
All roads lead to JavaRanch
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Hello Satou ,

When would declare="false" be used in that case?

Regards,
Shivani


/** Code speaks louder than words */
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

When the variable userInfo is already declared in the calling JSP.
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Originally posted by Satou kurinosuke:
When the variable userInfo is already declared in the calling JSP.


Yes, I tried declaring a string variable in the calling jsp and set declare="false" in the tag file. It is giving me the following error:

<<Variable name>> cannot be resolved.

Are you able to simulate this?
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Maybe you have used the NESTED scope, and used the variable in the tag's body, which could explain why you don't get errors. Could you please confirm by setting the scope to AT_END (or even AT_BEGIN).
Sorry for being unclear earlier: I was talking specifically about the NESTED scope... The attributes make sense (and are useful for non-scriptless pages) for AT_END and AT_BEGIN, but they're useless for NESTED variables because SimpleTags (hence tag files) cannot contain JSP scripting expressions - so Tomcat (and other containers?) never declare the scripting variable, and never read the attribute values. You can put variable-class="java.lang.List" for example, and everything works okay...

Without thinking, I've always declared the attributes before now (and probably will go on doing so for clarity) when most of the variables exposed by my actions are in fact NESTED (e.g. for iteration purposes). Also, I rarely use scripting variables anymore, instead preferring EL, so the sync. in this respect is irrelevant. I'm just surprised I'd never questioned it before really...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Yes, I think that the whole idea was to be able to tweak some variables using the AT_END or AT_BEGIN scope. For the NESTED scope, it doesn't make much sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tag File Variable Synchronisation
 
Similar Threads
Question about tag-files, simpleTags and classicTags context?
Cleared SCWCD 1.4 with 86%
Example of Dynamic attribute in Custom Tag
Draft objectives for SCWCD 1.4
JSP Implicit variables in tag files