permaculture playing cards*
The moose likes JSP and the fly likes JSTL, indirection of attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL, indirection of attributes" Watch "JSTL, indirection of attributes" New topic
Author

JSTL, indirection of attributes

Jiri Nejedly
Ranch Hand

Joined: Oct 22, 2002
Posts: 81
I would like to do following:
- I have one attribute, which name I know. This atribute's value is name of another attribute. And that atribute's value I would like to get.
I would write something like

${${my_attribute}}

, but EL syntax forbids that. Is it somewhat possible? Thanks.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
If I am understanding you correctly, you want to retrieve an attribute from some scope using the EL but you do not know the name of the attribute that you are trying to retrieve until runtime. For example, you have 3 attributes in request scope, attr1, attr2 and attr3 and sometimes you want to retrieve attr1, sometimes attr2 and sometimes attr3. If this is the case then you can use the [] operator. you can place into request scope another attribute (a String) that resolves to either attr1, attr2 or attr3. Let's say that attrName is the request scope attribute name that will indicate which of the 3 other attributes you will use. If attrName = "attr2" then $[attrName] (notice that there are no quotes around attrName) is the equivalent to writing $["attr2"]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

The actual notation would be something like:



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
Thanks Bear. I rushed the answer and forgot to designate the scope by using an implicit EL variable :-)
Jiri Nejedly
Ranch Hand

Joined: Oct 22, 2002
Posts: 81
Thanks, the [] oprator did the trick. The main reason for this is that I am using tag files:

http://www.oracle.com/technology/sample_code/tutorials/jsp20/tagfiles.html

Let's say I have a few jsp pages, each of them has the same sorting and filtering objects on it, but the objects (many lines of code) has their own names derived from the page name. So I write only one tag file, which renders filtering and sorting objects for all pages. Each page has now only one simple call to this tag file, with one parameter - page name. Every other atributes' names (which in my case are in session) I must derive only from page name. For example myPageFilter, myPageSort, myPageFilterValue... The other page mySecondPageFilter, mySecondPageSort, mySeconPageFilterValue... etc.

And that is why I needed that indirection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL, indirection of attributes
 
Similar Threads
HTTP Status 405 - HTTP method GET is not supported by this URL
Attributes for ServletConfig & ServletContext through web.xml
tag attribute declarations: defaultValue ?
Element values in different rows
Question on html struts tag