Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about tag-files, simpleTags and classicTags context?

 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I was reading in the JSP 2.0 Specification
that when creating a tag-file the container
will create a copy of the JspContext called
"JspContextWrapper". it says "must present
a clean page scope". What I understand
is that all page-attributes added before
invoking the tag-file will not make it
in the JspContextWrapper and that those
attibutes added to the JspContextWrapper
would be valid in the tag-file only. Can someone
confirm this?

For instance

Supporse I have a tag-file called "myTagFile.tag" like this

<jsp:useBean id="myBean" class="foo.MyBean" scope="page" />
<jsp:setProperty name="myBean" property="name" value="BBBB" />

My Attribute in Tag-File is ${pageScope.myPageAtt}<br>
My Bean name in Tag-File is ${pageScope.myBean.name}

Now suppose I have a calling JSP like this

<% pageContext.setAttribute("myPageAtt", "AAAA"); %>
My Attribute in calling JSP is ${myPageAtt}<br>
<prefix:myTagFile />
My Bean name in calling JSP is ${myBean.name}

The output would be:

My Attribute in calling JSP is AAAA
My Attribute in Tag-File is null
My Bean name in Tag-File is BBBB
My Bean name in calling JSP is null

Also Would this JspContextWrapper apply to SimpleTags
and classic tags too?

Thanks
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I understand
is that all page-attributes added before
invoking the tag-file will not make it
in the JspContextWrapper and that those
attibutes added to the JspContextWrapper
would be valid in the tag-file only. Can someone
confirm this?

That's correct - all page-scoped attributes in the calling JSP are isolated from all page-scoped attributes in the tag file.

The output would be:

My Attribute in calling JSP is AAAA
My Attribute in Tag-File is null
My Bean name in Tag-File is BBBB
My Bean name in calling JSP is null

This is the output I would expect.

Also Would this JspContextWrapper apply to SimpleTags
and classic tags too?

No. The short answer is that this is only mandated in the Tag Files section of the specification. The longer explanation is that tag files need to have their 'own scope' because of things like attributes - all attribute values are placed in the Context Wrapper's page-scope when the tag is invoked, and so to avoid conflicts with page-scoped attributes in the page, the wrapper is essential. This is distinct from a normal tag handler implementation (either classic or SimpleTag) because here you implement JavaBean setter methods to set attributes, so no 'special page-scope' is required. The next question you'll ask is how to actually export a variable from the tag file, for instance in your previous example, how could we actually get "My Bean" to be the value of the bean in the calling JSP page? The answer is synchronisation, confusingly a different kind to that using in regular classic handlers, but all explained in my book (even though it isn't on the exam)!
[ April 19, 2006: Message edited by: Charles Lyons ]
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thank you very much this answer makes it very clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic