Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tag File Variable Synchronisation

 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Satou ,

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

Regards,
Shivani
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the variable userInfo is already declared in the calling JSP.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic