• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

tag file question

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1- "You can do scripting in tag file."
2- "You can�t do scripting inside the body of the tag".

1-
<myTagFileExample1:frag att="Is this code valid" />
and:
<%@ attribute name="att" required="true" %>
Hey, <%=att%> ???

- is it valid??


2- <myTagFileExample2:frag>
<%="could it take scripting elements at this point?" %>
</myTagFileExample2:frag>

tagfile:
<%@ tag body-content="scriptless" %>
Let me do a question...<jsp oBody/>

Tks
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit confused as to what's what in this question: which bits are in the JSP and which are in the tag file?

I think the essence of your question regards the body content of tags implemented by tag files: the answer is any tag implemented as a SimpleTag (including tag files) must not contain JSP scripting elements in their body. So for example, if the <myTagFileExample2:frag /> is a tag file (or SimpleTag) taking a body, then this is invalid:

<myTagFileExample2:frag>
<% scriplet %>
<%= expression %>
</myTagFileExample2:frag>

This will cause a translation error because scripting element(s) are declared in the body. There are two explanations here: SimpleTags use a JspFragment to control the output of their body, but JspFragments (unlike BodyContent) don't push a new JspWriter onto the 'out' stack. It is therefore dangerous to use an expression inside the body of a SimpleTag/tag file because this would be written directly to 'out' and wouldn't be encapsulated in the JspFragment for later invocation. The second reason is that SimpleTag only has the doTag() method of interest, so cannot cause the container to synchronise between scoped attribues in the SimpleTag/tag file and scripting variables in the JSP page... Therefore, scripting variables are useless because they can't be updated by a Simpletag.

You will note however that EL statements are valid in SimpleTag bodies, due to the different way in which they are invoked, and because they only ever reference scoped attributes/EL implicit objects, which are not as 'tied down' to the JspPage implementation as scripting variables are. Synchronisation on scoped attributes is much easier to perform from a tag file (using the JspContext/PageContext or the <c:set> JSTL action).
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Felipe,

I agree with Charles concerning the content of a the body of a tag.

Concerning scripting inside a Tag File, yes, it is allowed: have a look at p500 of the HFSJ, 1rst and 2nd Q:A

Cheers
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the number one is valid
since the script expression is
in the tag-file itself.

The second one is invalid
because the script expression is
in the body of the calling tag.

I believe the idea is avoid
as much as possible scriptlets

so in your first code you can use

<myTagFileExample1:frag att="Is this code valid" />
and:
<%@ attribute name="att" required="true" %>
Hey, ${att}

and should work the same.

The body content of a tag-file by default is
scriptless. You do not need the directive
in your second snippet.
reply
    Bookmark Topic Watch Topic
  • New Topic