This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes How to Set HashMap in request attribute using JSTL Tag 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 "How to Set HashMap in request attribute using JSTL Tag" Watch "How to Set HashMap in request attribute using JSTL Tag" New topic
Author

How to Set HashMap in request attribute using JSTL Tag

ShaikSalam Bashoeb
Greenhorn

Joined: Dec 10, 2008
Posts: 24
Hello,

Can you tell me how to set HashMap in request attribute using JSTL tag.. I am able to set HashMap in scriptlet and Iterate successfully, But when i try to set HashMap in request attribute using JSTL tag. I am getting the following error

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Why are you trying to do this in the first place? The JSTL and EL are meant to replace scriptlets, not work with them.

Tasks like setting maps up as scoped variables for use in the JSP is the job of the controller, not the JSP itself.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ShaikSalam Bashoeb
Greenhorn

Joined: Dec 10, 2008
Posts: 24
Actually I am working on very big project. I need to prepare one HashMap in jsp and set it again in request attribute, so that I can use that hashmap for remaining page of the jsp...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Why bother? Since you're hard-coding the values into the JSP to being with, why not just use those values directly in the HTML rather than going through the overhead of the Map? You're introducing a lot of complexity that isn't necessary.

If this is just an example and the values are not hard-coded, why isn't this being done in the controller?

Or, if you insist on doing it this way (not recommended), just uncomment the setAttribute() call and forget using the JSTL for this -- working with scripting variables is not what the JSTL was meant for. It's like using a nail file to comb your hair.
ShaikSalam Bashoeb
Greenhorn

Joined: Dec 10, 2008
Posts: 24
I completely agree with your comments that we should not mix both scriptlet and JSTL But my intension was to ask that in JSTL there is


which is used to set the Attribute that we will do it from JSP page,
I also need to set one Attribute of HashMap that is why i asked , is there any way to set HashMap from JSP page using JSTL.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Not if the Map is only available as a scripting variable. Scripting variables are invisible to the JSTL as, again, they are not intended to be used in concert.

JSP Rule #1: if it's hard to do on a JSP page with the JSTL/EL, there's a good chance that it's not something that you should be doing in a JSP in the first place.

If you are going to use scriptlets to create the Map on the page (still a futile exercise, in my opinion), then use scriptlets to create the scoped variable. Don't try to shoe-horn the wrong tool into the mix.
ShaikSalam Bashoeb
Greenhorn

Joined: Dec 10, 2008
Posts: 24
Thanks Bear Bibeault for your quick responses and accurate.

I have understood that if we want to create Map and set in a request scope as a attribute then we should scriptlet.
NO way to do with JSTL..

Thanks again...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Actually, in modern web apps, nothing should be done in scriptlets. But if you are going to use them (not recommended), don't try to mix them with the JSTL and EL. They do not mix well.
 
jQuery in Action, 2nd edition
 
subject: How to Set HashMap in request attribute using JSTL Tag
 
Similar Threads
Using JSTL and Struts
struts dynavalidatorBean with HashMap & JSTL
problem with c:set tag
c:out doesn't print values as expected
<c:set Usage