Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Question about tag-files, simpleTags and classicTags context?

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Thank you very much this answer makes it very clear.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic