Why does my conditional display of 2D array elements fail? - jstl

I have a 2-dimensional array, which can be done like this (working code):
<c:forEach items="${dataArray}" var="row">
Subject id: ${row[0]}
</c:forEach>
But I want to add a condition; when I try this it does not display anything:
<c:forEach items="${dataArray}" var="row">
<c:choose>
<c:when test="${fn:containsIgnoreCase(${row[0]}, 'condtion1')}">
Subject id: ${row[0]}
</c:when>
</c:choose>
</c:forEach>
Have I got something wring in the test attribute that makes it always false?

Related

How to get values for dynamic form field names using JSTL and EL

I have 8 checkboxes and 1 button, when the user check any of the checkboxes and click the button, I want to check if any of the checkbox is checked and display it in another .jsp
I have already referred to few similar questions with no luck so far. So i tried to manage with my own logic
First.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<input type="checkbox" id="seat" name="seat${loop.index}" value="seat${loop.index}" >
<label for="seat">Seat${loop.index}</label>
</c:forEach> <br> <br>
<input type="submit" value="Save" name="savebtn">
Second.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<c:if test="${not empty param.seat[loop.index]}">
<c:out value="${param.seat1} is booked"/>
</c:of>
</c:forEach>
I have 2 problems regarding the code above :
i can't get loop.index value inside the param $param.seat[loop.index] doesn't work
And even if i try to do it manually, i can only get value from seat1. I can't get value from the rest ( seat2, seat3, etc).
${param.seat[loop.index]} implies that seat is a collection, which it is not (it probably does not even exist). You are after ${param.seatX}, where you can dynamically set X. You can do that by creating a variable containing the parameter name first:
<c:set var="seatVarName" value="seat${loop.index}"/>
Now you can use this variable to get the parameter value from the implicit EL object:
${param[seatVarName]}
See also:
JSP expression language and dynamic attribute names

bean value show correctly, but add logic in jsp and it fails

The property is set in the bean, updated in the DAO, and appears correctly in the jsp when set as the following:
<html:hidden property="user.strInfoLocked" value="${user.strInfoLocked}" />
When I use developer tools, the value shows as follows:
<input type="hidden" name="user.strInfoLocked" value="true">
But I can't get the value to be used in a conditional statement. I've tried this:
<c:choose>
<c:when test="${user.strInfoLocked eq 'true'}">TRUE </c:when>
<c:when test="${user.strInfoLocked eq 'false'}">FALSE </c:when>
</c:choose>
and I've tried this:
<c:if test= "${user.strInfoLocked == 'true'}">
<p>My value is TRUE<p>
</c:if>
<c:if test= "${user.strInfoLocked == 'false'}">
<p>My value is FALSE<p>
</c:if
and in both cases, the whole thing is skipped.
Do I need to declare this somewhere else, apart from the bean? It's just odd that the values are showing up correctly, but then when I add it to logic, the whole thing goes bezerk.
Both eq and == should work. I suggest you display the value from user.strInfoLocked and be sure that it has the value 'true' or 'false'.
Boolean should work, too, if you define the field in your bean as boolean.
<c:choose>
<c:when test="${result.strInfoLocked}">Locked</c:when>
<c:when test="${!result.strInfoLocked}">Not Locked</c:when>
</c:choose>
I have tested the code as a string and as a boolean. Both work.
This seems to be a type. You missed out space before eq in below statement and you need eq for the same:
<c:choose>
<c:when test="${user.strInfoLockedeq 'true'}">TRUE </c:when>
<c:when test="${user.strInfoLockedeq 'false'}">FALSE </c:when>
</c:choose>
Chnage it to :
<c:choose>
<c:when test="${user.strInfoLocked}">TRUE </c:when>
<c:when test="${user.strInfoLocked}">FALSE </c:when>
</c:choose>

String checking in jstl error

I have to check a attribute from server , and depending upon the value, i have to set the color of the font. The code i used for the same is: I am very new in jstl. Can handle this with javascript or jQuery, but have a constraint , not to use js here. :(
<c:if test="${error != null}">
<c:choose>
<c:when test="${error_code eq 'failed'}">
<p align="left"><font size="3" color="red">${error}</font></p>
</c:when>
<c:when test=test="${error_code eq 'success'}">
<p align="left"><font size="3" color="green">${error}</font></p>
</c:when>
</c:choose>
</c:if>
And the server side code is:
if (result) {
request.setAttribute("error_code","success");
request.setAttribute("error","Object successfully created.");
} else {
request.setAttribute("error_code","failed");
request.setAttribute("error","Object creation failed.");
}
what i am doing wrong here:
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
org.apache.jasper.JasperException: /tool/Content/content.jsp (li
ne: 68, column: 18) **quote symbol expected**
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorH
andler.java:42)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.j
ava:443)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.j
ava:89)
Following line is incorrect. You have write "test" two times ;-)
<c:when test=test="${error_code eq 'success'}">
Now its working , I changed the code as:
<c:if test="${error != null}">
<c:choose>
<c:when test="${fn:containsIgnoreCase(error_code, 'failed')}">
<p align="left"><font size="3" color="red">${error}</font></p>
</c:when>
<c:when test="${fn:containsIgnoreCase(error_code, 'success')}">
<p align="left"><font size="3" color="green">${error}</font></p>
</c:when>
</c:choose>
</c:if>

iterate collection and display in html row jstl

I have searched through for this logic pretty much everywhere but am sure I haven't done a great job and hence this question.
I have an Arraylist and it needs to be iterated and displayed in the following format
User1 User2 User3
User4 User5 User6
User7 User8 User9
I am able to do this using normal JSP scriptlets but not using JSTL. I have tried using JSTL but it skips a few entries from the list. Here is my code
<tr>
<c:forEach var="party" items="${partiesList}">
<c:set var="ctr" value="${ctr+1}" scope="page"/>
<c:choose>
<c:when test="${ctr le 3}">
<td>
<figure>
<img src="images/${party.avatarUrl}" onclick="showHide('','${party.screenName}')" class="avaimg" alt="${party.screenName}">
<figcaption><spring:message code="user.${party.screenName}" text="${party.screenName}"/></figcaption>
</figure>
</td>
</c:when>
<c:otherwise>
</tr>
<c:set var="ctr" value="0" scope="page"/>
</c:otherwise>
</c:choose>
</c:forEach>
Any help would be appreciated.
Thanks in advance
You can do this by using the varStatus attribute and the modulo operator (%).
<table>
<c:forEach var="party" items="${partiesList}" varStatus="status">
<c:if test="${status.index % 3 == 0}"><tr></c:if>
<td>${status.index}</td>
<c:if test="${status.index % 3 == 2}"></tr></c:if>
</c:forEach>
</table>
varStatus will return a LoopTagStatus object, where index is
The index of the current round of the iteration. If iteration is being performed over a subset of an underlying array, java.lang.Collection, or other type, the index returned is absolute with respect to the underlying collection. Indices are 0-based.
You can use the modulo operator to check if you need to start or close a new row.
A different approach would be just looping through the list, output it as an unnumbered list and use CSS to style it the way you want. That's how I would have solved it. An example of how your CSS could look:
ul.my-class { overflow: hidden; width: 600px; }
ul.my-class li { float: left; width: 200px; }

Accessing HashMap in EL with string + int key

Is there a way to access HashMap element in EL with key which is concatenation of String and int value. Something like this:
<c:forEach begin="1" end="5" var="current">
<c:out value="${myHashMap['elem-' + current]}"/>
</c:forEach>
This code can accomplish desired behavior
<c:forEach begin="1" end="5" var="current">
<c:set var="key" value="elem-${current}" />
<c:out value="${myHashMap[key]}"/>
</c:forEach>

Resources