aspose file tools*
The moose likes JSP and the fly likes Attribute Names and JSTL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Attribute Names and JSTL?" Watch "Attribute Names and JSTL?" New topic
Author

Attribute Names and JSTL?

James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
I am by no means a JSTL expert, but I have been reading up on it extensively. My concern is with the expression language. Suppose I have a shopping cart object (to borrow an example from Shawn Bayern's JSTL In Action) stored in the user's session as an attribute named "shoppingCart". I could access that object via the following expression...
${sessionScope.shoppingCart}
However, from my understanding, you are supposed to name your attributes like you name your classes...
session.setAttribute( "com.mycompany.mypackage.ShoppingCart", shoppingCart );
So, how would this translate into a JSTL expression...
${sessionScope.shoppingCart.com.mycompany.mypackage.ShoppingCart}
But, wouldn't JSTL think you are talking about the ShoppingCart property of the mypackage property of the mycompany property of the com property of the shoppingCart object stored in session scope? It seems that only simple attribute names are supported by the JSTL. Is this true? Does this introduce a diffucult situation trying to keep things straight? Even if you can manage it at the session scope level (which pertains to only one user), what about application scope (attributes on the ServletContext)? It seems that this could get ugly really quickly. Any thoughts, comments?


James Carman, President<br />Carman Consulting, Inc.
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Originally posted by James Carman:
However, from my understanding, you are supposed to name your attributes like you name your classes...
session.setAttribute( "com.mycompany.mypackage.ShoppingCart", shoppingCart );
So, how would this translate into a JSTL expression...
${sessionScope.shoppingCart.com.mycompany.mypackage.ShoppingCart}

Indeed, that naming convention is suggested by the Servlet specification. It need not be followed, but it helps avoid naming clashes. I personally think it's useful, and tend to adopt the following convention: any reusable components, intended to be applied in multiple applications, should always use fully qualified names (com.foo...). I have no problem when an application's own internal data, by contrast, uses simple, unqualified names. Again, though, that's just my personal aesthetic.
As for how to address variables with package-qualified names in JSTL, you're correct that the expression you listed won't do it. Instead, you need to use the "[]" notation:
${sessionScope["edu.yale.its.foo.bar"]}
Note that you need to use quotation marks that don't clash with the attribute value -- e.g.,
a="${sessionScope['foo']}"
or
a='${sessionScope["foo"]}'
Some early adopters of JSTL that I've worked with tend to like to move data like this into a local page-scoped variable for convenience. E.g.:
<c:set var="cart" value="${sessionScope['com.foo.bar.shoppingCart']}" />
. . .
<cut value="${cart.foo}" />
They do this just for convenience, to avoid excessive typing (and the possibility of typographical errors).
Hope that helps!
[ September 06, 2002: Message edited by: Shawn Bayern ]

Shawn Bayern<br />"JSTL in Action" <a href="http://www.jstlbook.com" target="_blank" rel="nofollow">http://www.jstlbook.com</a>
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Okay, so I can "fully qualify" attribute names in JSTL. Now that you've pointed out the solution, I remember reading about it in your book. Oops. Anyway, what about getting the value of static variables of a class...
public interface Attrs
{
public static final String ATTR_CUSTOMER_RECORD = Attrs.class.getName() + ".CUSTOMER_RECORD";
public static final String ATTR_QUERY_RESULT = Attrs.class.getName() + ".QUERY_RESULT";
}
I usually do something like this to keep my JSP pages and my servlets in sync (not like the band). :-) Can I use these values in an expression?
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Originally posted by James Carman:
Okay, so I can "fully qualify" attribute names in JSTL. Now that you've pointed out the solution, I remember reading about it in your book. Oops. Anyway, what about getting the value of static variables of a class...
public interface Attrs
{
public static final String ATTR_CUSTOMER_RECORD = Attrs.class.getName() + ".CUSTOMER_RECORD";
public static final String ATTR_QUERY_RESULT = Attrs.class.getName() + ".QUERY_RESULT";
}
I usually do something like this to keep my JSP pages and my servlets in sync (not like the band). :-) Can I use these values in an expression?

No, the JSTL expression language is oriented at beans, not classes, using the Introspector, not generalized reflection. Therefore, you can't get static properties. (There's no way to name a 'class' abstractly either.)
Instead, simply use ServletContext initialization parameters or provide accessor methods (getQueryName() and so forth).
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
That's cool. No biggie, I was just wondering.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Attribute Names and JSTL?