8

I'm looking to set the key value pairing of a HashMap using JSTL only. Is this possible?

I know how to retrieve the key value pairs, but I haven't found a way to set them.

Any help would be appreciated.

Example of retrieving HashMap key/value pairs using JSTL:

<c:forEach var="hash" items="${myHashMap}">             
    <c:out value="${hash.key}" />
    <c:out value="${hash.value}" />
...
Ruepen
  • 411
  • 5
  • 8
  • 19

2 Answers2

20

You can use the <c:set>.

<c:set target="${myHashMap}" property="key" value="value"/> 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
evanwong
  • 5,054
  • 3
  • 31
  • 44
0

I wouldn't use JSTL to do that, but straight-up JSP will get it done...

<%
myHashMap.put("hello", "world");
%>
Madbreaks
  • 19,094
  • 7
  • 58
  • 72
Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • Ooh. The evanwong answer is more JSTL-y, but I like straight-up JSP. – Bob Kuhar Jan 05 '12 at 22:00
  • 1
    1) You are not answering the concrete question (exclusively using JSTL). 2) The code example in your answer is incomplete. 3) scriptlets are [discouraged](http://www.oracle.com/technetwork/articles/javase/code-convention-138726.html) since a decade. This was not a hate. The answer is simply not useful (as the downvote tooltip says). By the way, were you lying [here](http://stackoverflow.com/a/8682774/157882) where you said to not care about downvotes? :) You get sympathy upvotes soon enough. – BalusC Jan 05 '12 at 22:05
  • @BalusC I strongly disagree. JSTL does not exclude old school scriptlet, it absolutely works with it. My example is complete and works within the context of the question which sounds, to me, as a useful solution. It event takes less code and would make it through a refactoring. The downvote for a working solution surprised me. The "hate" comment was to keep the air light. I guess I failed. – Bob Kuhar Jan 05 '12 at 22:18