JSP and hashmap's containsKey

L

lord.zoltar

Hello,
I have a bean on my JSP page which is a HashMap. I would like to check
to see if it contains an key, and if so, print a value. In Java, it
would probably look something like:

if (myHashMap.containsKey(k))
system.out.println(myHashMap.get(v));

I've tried <logic:present name="myHashMap" property="<%= keyVal %>">
but think this is actually looking for a getter method "getSomething
()" on myHashMap (Assuming that keyVal evaluates to "something").
Is there a JSP/Struts tag that can access containsKey?
 
L

Lew

Hello,
I have a bean on my JSP page which is a HashMap. I would like to check
to see if it contains an key, and if so, print a value. In Java, it
would probably look something like:

if (myHashMap.containsKey(k))
    system.out.println(myHashMap.get(v));

I've tried <logic:present name="myHashMap" property="<%= keyVal %>">
but think this is actually looking for a getter method "getSomething
()" on myHashMap (Assuming that keyVal evaluates to "something").
Is there a JSP/Struts tag that can access containsKey?

Avoid scriptlet in JSPs.

Avoid Struts tags where STL or EL exist to do the job.

Try this (untested here):

<c:if ${ ! empty myMap["keyValue"]} >
<c:eek:ut value='${myMap["keyValue"]}' />
</c:if>
 
L

lord.zoltar

Hello,
I have a bean on my JSP page which is a HashMap. I would like to check
to see if it contains an key, and if so, print a value. In Java, it
would probably look something like:
if (myHashMap.containsKey(k))
    system.out.println(myHashMap.get(v));
I've tried <logic:present name="myHashMap" property="<%= keyVal %>">
but think this is actually looking for a getter method "getSomething
()" on myHashMap (Assuming that keyVal evaluates to "something").
Is there a JSP/Struts tag that can access containsKey?

Avoid scriptlet in JSPs.

Avoid Struts tags where STL or EL exist to do the job.

Try this (untested here):

 <c:if ${ ! empty myMap["keyValue"]} >
   <c:eek:ut value='${myMap["keyValue"]}' />
 </c:if>

Thanks!
I'm supporting an older app which currently does not use any JSTL. How
much trouble is it to include JSTL? Would new libraries need to be
added for deployment? That would involve getting approval for new
dependencies... ugh... this was supposed to be a very small piece of
maintenance work.
I was wondering if it was possible to do this in plain old struts. If
not I will just write a custom tag (they are already accepted in this
app).
 
L

Lew

Lew said:
Try this (untested here):
 <c:if test='${ ! empty myMap["keyValue"]}' >
   <c:eek:ut value='${myMap["keyValue"]}' />
 </c:if>

Please do not quote sigs.

I'm supporting an older app which currently does not use any JSTL. How
much trouble is it to include JSTL? Would new libraries need to be
added for deployment? That would involve getting approval for new
dependencies... ugh... this was supposed to be a very small piece of
maintenance work.

It is.
I was wondering if it was possible to do this in plain old struts [sic]. If
not I will just write a custom tag (they are already accepted in this
app).

JSTL is just a pair of JARs, IIRC. EL uses just one, I think. They
can go in the app server (e.g., Tomcat); they don't necessarily go in
the application. If your app server doesn't support JSTL and EL, it's
too old anyway.

If you don't have them, you don't need to write a custom tag. It's
better to load the desired result as a request attribute in the
controller part of your MVC architecture anyway, and Struts has
conditional tags that will work in lieu of <c:if>. If you can't get
at EL, you can use <jsp:useBean>.

But really, there's no good reason not to have JSTL and EL available.
They were released five and a half years ago already, in J2EE 1.4/JSP
2.0.
<http://en.wikipedia.org/wiki/Java_EE_version_history>
<http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html>
<http://java.sun.com/j2ee/1.4/docs/tutorial/doc/
JSPIntro7.html#wp71019>
<http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSTL.html#wp74644>
 
A

Arne Vajhøj

Lew said:
Lew said:
Try this (untested here):

<c:if ${ ! empty myMap["keyValue"]} >

that should be

<c:eek:ut value='${myMap["keyValue"]}' />
</c:if>

There are plenty of alternatives anyway. See demo below.

Arne

===============================================

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*" %>
<%
Map m = new HashMap();
m.put("k", "val");
request.setAttribute("m", m);
%>
Lew:
<c:if test='${! empty m["k"]}' >
<c:eek:ut value='${m["k"]}' />
</c:if>
<br>
Alternative 1:
<c:if test='${m["k"] != null}' >
<c:eek:ut value='${m["k"]}' />
</c:if>
<br>
Alternative 2:
<c:if test='${! empty m.k}' >
<c:eek:ut value='${m.k}' />
</c:if>
<br>
Alternative 3:
<c:if test='${m.k != null}' >
<c:eek:ut value='${m.k}' />
</c:if>
<br>
Alternative 4:
<c:eek:ut value='${m["k"]}' default=''/>
<br>
Alternative 5:
<c:eek:ut value='${m.k}' default=''/>
<br>
Alternative 6:
<c:eek:ut value='${m["k"]}' />
<br>
Alternative 7:
<c:eek:ut value='${m.k}' />
<br>
Alternative 8:
${m["k"] != null ? m["k"] : ""}
<br>
Alternative 9:
${m.k != null ? m.k : ""}
<br>
Alternative 10:
${m["k"]}
<br>
Alternative 11:
${m.k}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top