wood burning stoves 2.0*
The moose likes JSP and the fly likes setting a dynamically named scope attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "setting a dynamically named scope attribute" Watch "setting a dynamically named scope attribute" New topic
Author

setting a dynamically named scope attribute

JohnWilliam Fitz
Ranch Hand

Joined: Oct 13, 2008
Posts: 80
Hi!

I want to set a session scope attribute in my jsp, but I will only know the name of the attribute to set at runtime (long story).

I first tried something like this...

<c:set var="prefix" value="${something}" />
<c:set var="attribName" value="${prefix}Attrib" />
<c:set var="${attribName} value="howdy" />

...but alas <c:set> does not accept expressions for the var attribute (which I knew but forgot).

So, before I go writing my own custom tag to do this, I'd like to know: Does something already exist in JSTL or the standard custom tag library <jsp:xxx> to do this? Or perhaps in struts 1.x (which I am using in this project)?

Thanks muchly!
John
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Is there a reason that you are doing this in the JSP rather than the controller?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
JohnWilliam Fitz
Ranch Hand

Joined: Oct 13, 2008
Posts: 80
Yes. ;o)

Should I write my own tag?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

JohnWilliam Fitz wrote:Yes. ;o)

/unsubscribes
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why is the name of the attribute dynamic? Why not just set a known name on the Java side with the value you need?
Victor Williams Stafusa da Silva
Ranch Hand

Joined: May 24, 2007
Posts: 34
I got the same problem. Believe it. There is a reason for this.

Let's say you got a tag-file:



I.E. The purpose is to set some value taken from EL or some HTML and make it visible to the caller. Using taglibs is not good enough, because handling the HTML in java code sucks. Normally, the HTML and the inserted value should be unentangled, but if the inserted value is a bunch of complicated HTML, it simply can't be well unentangled.

But, it is very simple to write a SimpleTag to simulate a <c:set> where the var is rtexprvalue. So, in my opinion, <c:set> should have the var attribute with rtexprvalue="true", so nobody would need to reinvent the well in this point.


SCJP 6.0 - 88%, SCWCD 5 - 79%, SCJA - 96%, SCSNI - 68%, SCBCD 5 - 95%
JohnWilliam Fitz
Ranch Hand

Joined: Oct 13, 2008
Posts: 80
Why? I'm in a contest for the worst programmer of the month at my company and the competition is stiff. ;o)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: setting a dynamically named scope attribute
 
Similar Threads
JSTL (c:set)
Need help with JSTL: setting end attribute dynamically of "substring" tag
how to use scope in target version of c:set
c:set using var
How can I c:set an int in my JSP?