treasureireland
treasureireland

Reputation: 87

JSTL c:if statement not working

I want to be able to send an alert if a query string exists, but I dont need to know the value it returns, just that it exists.

I tried this, but it doesnt work:

<% 
request.setAttribute("appInfo", request.getParameter("appInfo"));
%>
<c:if test="${appInfo}">
<script>
alert("${appInfo}");
</script>
</c:if>

I also tried this (with the url being ?appInfo=88) and it worked.

<%
request.setAttribute("appInfo", request.getParameter("appInfo"));
%>
<c:if test="${appInfo == 88}">
<script>
alert("${appInfo}");
</script>
</c:if>

I dont understand why solution 2 works but 1 doesnt, any ideas?

Upvotes: 0

Views: 1411

Answers (1)

Jacob Mattison
Jacob Mattison

Reputation: 51052

c:if doesn't work like a Javascript if, where non-null values will be regarded as "truthy". For c:if you need a result that actually is true or false. You can do

<c:if test="${appInfo != null}">

to check if the value is there.

Upvotes: 1

Related Questions