aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question about jsp:attribute body Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question about jsp:attribute body" Watch "Question about jsp:attribute body" New topic
Author

Question about jsp:attribute body

Ernesto Leyva
Ranch Hand

Joined: Feb 23, 2006
Posts: 62
Some one knows if I can put JSP scriptlets in a
<jsp:attribute> or is it scriptless content only?

Tahnks
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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).


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Ernesto Leyva
Ranch Hand

Joined: Feb 23, 2006
Posts: 62
Thanks again Charles

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

Thanks
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
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


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

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.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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>
Jigar Gosar
Ranch Hand

Joined: Jul 09, 2002
Posts: 95

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 ]

Regards,<br />Jigar Gosar (SCJP 1.4)<br />visit: <a href="http://jigar.org/blog" target="_blank" rel="nofollow">http://jigar.org/blog</a>
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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

Joined: Dec 04, 2004
Posts: 950
Thanks to all for such in-depth explanation. I was too much confused.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about jsp:attribute body