• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question about jsp:attribute body

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some one knows if I can put JSP scriptlets in a
<jsp:attribute> or is it scriptless content only?

Tahnks
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Some one knows if I can put JSP scriptlets in a
<jsp:attribute> or is it scriptless content only?



This depends on what tag you are supplying the content to: if it's a classic tag, scripting elements are permitted. If it's a SimpleTag, then a <jsp:attribute> cannot contain a scriptlet. If it's an element you're creating using <jsp:element>, then scripting elements are permitted as content of an enclosed <jsp:attribute> since a <jsp:element> is a standard action (it isn't actually implemented as a tag handler).
 
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
Thanks again Charles

Seems you can explain issues very easy. May be it is a good idea to check your book.

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


Originally posted by Charles Lyons:


This depends on what tag you are supplying the content to: if it's a classic tag, scripting elements are permitted. If it's a SimpleTag, then a <jsp:attribute> cannot contain a scriptlet. If it's an element you're creating using <jsp:element>, then scripting elements are permitted as content of an enclosed <jsp:attribute> since a <jsp:element> is a standard action (it isn't actually implemented as a tag handler).



Still confused on this. Thouhg the <jsp:attribute> directly encloed in the tag, It is not exactly the body of the tag. The attributes are processed before the body of the tag. I think it is related to the rtexprvalue attribute of the <attribute> tag or <@ attribute> directive> in the tag files.

Please Clarify

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


The attributes are processed before the body of the tag.



Yes you are correct. But <jsp:attribute> is an alternate way to put attribute in and don't count as body-content.

There is also a restriction with this tag. Only one attribute of the enclosing tag can be put with this <jsp:attribute> tag.
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thouhg the <jsp:attribute> directly encloed in the tag, It is not exactly the body of the tag. The attributes are processed before the body of the tag. I think it is related to the rtexprvalue attribute of the <attribute> tag or <@ attribute> directive> in the tag files.


Ooops! Sorry about my earlier post, I was being dense and probably thinking of <jsp:body> rather than <jsp:attribute>. Sorry again.

My previous explanation was correct for <jsp:body>. The interesting thing about attributes is because they are processed before the tag handler is invoked, they can always contain any scripting elements you want. For example:

<mytag attr="<%= date %>" />

is perfectly valid, regardless of whether this is a classic or simple tag or a tag file (obviously this is invalid in a JSP document for several reasons)! The only requirement is that the attribute be dynamic so it can accept request-time expressions.

So Narendra was correct: the content of <jsp:attribute> can be anything regardless of the tag type, provided that attribute is configured as rtexprvalue="true" (either in the tag file or in the TLD).

NOTE: There is one restriction. If the attribute is declared to be a fragment (i.e. to be converted to an instance of JspFragment), it (a) must be declared using <jsp:attribute> rather than on the tag itself; (b) must have scriptless body content (so no JSP scripting elements but EL is permitted). This is due to the way in which JspFragments are processed (recall a JspFragment also forms the body of a SimpleTag/tag file, hence the restriction applies there as well).

NOTE: The <mytag attr="<%= date %>" /> is illegal in a JSP document because the < character should be used only for the start of elements (instead you should use a < entity reference). You might try:

<mytag attr="<jsp:expression>date</jsp:expression>" />

But this is illegal because elements can't be placed in attribute values. The valid alternative is therefore:

<mytag>
<jsp:attribute name="attr"><jsp:expression>date</jsp:expression></jsp:attribute>
</mytag>
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


NOTE: The <mytag attr="<%= date %>" /> is illegal in a JSP document because the < character should be used only for the start of elements (instead you should use a < entity reference). You might try:

<mytag attr="<jsp:expression>date</jsp:expression>" />

But this is illegal because elements can't be placed in attribute values. The valid alternative is therefore:

<mytag>
<jsp:attribute name="attr"><jsp:expression>date</jsp:expression></jsp:attribute>
</mytag>



well i assume "JSP document" means xml representation of jsp. and hence


would be invalid.

but


is not the only solution, i think even this can be used


correct me if i am wrong.
[ April 22, 2006: Message edited by: Jigar Gosar ]
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, a "JSP document" is the XML-formatted variety of JSP.

I think even this can be used:

correct me if i am wrong.



I don't think so - I believe it would be interpreted as a string literal with the value "%= date %" and not an expression. Why not try this in your container and see what happens, using a JSTL Core tag for example? I think you'll find that using <jsp:attribute> is the only way to supply JSP scripting expressions to tag attributes in JSP documents. Alternatively, undertake the better practise of only using EL expressions unless you need to alter data (in this case, probably a Front Controller servlet would be a better idea).
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for such in-depth explanation. I was too much confused.
 
What a show! What atmosphere! What fun! What a tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic