• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL, indirection of attributes

 
Jiri Nejedly
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The actual notation would be something like:

 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear. I rushed the answer and forgot to designate the scope by using an implicit EL variable :-)
 
Jiri Nejedly
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic