• Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp:usebean and Maps

 
Rick DeBay
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to initialize a Map within a jsp:useBean tag?
jsp:useBean id="map" scope="session" class="java.util.HashMap" type="java.util.Map"
jsp:setProperty name="homeCatParamMap" property="s" value="true"
c:set target="${map}" property="s" value="true"
/jsp:useBean
Neither initializer works. The jsp:setProperty returns an error that s isn't a property of HashMap, and the JSTL c:set probably doesn't work because the HashMap hasn't been placed in scope yet.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jsp:setProperty will not work since Map is not bean-patterned. It's looking for a method named setS().
The JSTL is a bit more interpretive. The following worked famously for me:

[ February 19, 2004: Message edited by: Bear Bibeault ]
 
Rick DeBay
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jsp:setProperty will not work since Map is not bean-patterned

Right. I tried it hoping it would, as the spec makes noises about it working with indexed properties, and I was hoping the implementers would throw in mapped properties.
...

Your c:set is not in the body of the jsp:useBean. I'm trying to initialize the Map if it hasn't been created.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the example to:

introduced no problems.
Here's the relevant code from the Tomcat-generated JSP servlet that shows that it does what you want:

What happens when you try to execute my example? (I'm running Tomcat 5.0.18, btw).
 
Rick DeBay
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D*mn if it isn't working now. It's like bringing your car to the mechanic, whenever you show a problem to someone it goes away. Thanks for your help.
Now if I could only get you to look at the crabgrass on my lawn...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't do lawns!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!