How to give c:if tag in display tag - jstl

I have a display tag in my jsp file. Its like..
<display:table id="currentRow" name="${ListObj}" requestURI="" sort="page" defaultsort="2"
pagesize="5" class="displayTable">
<display:caption><font color="red">Users List</font></display:caption>
<display:column property="ID" title="Role" ></display:column>
<display:column property="Name" title="User Name" sortable="true"></display:column>
<c:if test="%{currentRow.ID ne '1'}">
<display:column >
<i>edit</i>
</display:column>
</c:if>
</display:table>
i had wrote the code <c:if test="%{currentRow.ID ne '1'}"> for that i didnt want to show the edit link for user with ID 1. But that condition is not working. Ie no rows in display tag shows edit link. But if i give <c:if test="%{currentRow.ID eq '1'}">, edit link will be displayed.
How can i make it displayed for all the rows except the one with ID=1???

You should put the if statement inside the <display:column> tag as you will always want the <td> tag rendered in the table even if it is empty.
<display:column >
<c:if test="%{currentRow.ID ne 1}">
<i>edit</i>
</c:if>
</display:column>
If the id attribute is an integer you will want to perform equals on it as an int.

Related

JSTL - Promotional Code

I'm trying to use JSTL, so that if a user enters a code, it is looked up in the database, if it equals the value, then the following discount is applied.
I kind of have an idea/pseudocode of what i want to write but i cant get it working or where i should go from here
In my purchase page i have:
form action="promo.jsp" method="GET">
Promotional Code: <input type="text" name="promo">
<input type = "submit" id="promo" value = "Apply Code" />
</form>
<form action="complete_purchase.jsp" method="POST">
<input type="submit" value="Complete Purchase" />
</form>
And in my "promo.jsp"
I have something a long the lines of:
<sql:query var="item">
select item_price from inventory;
</sql:query>
<sql:query var="pro">
select code,discount from promotion_code;
</sql:query>
<c:if test="${pro.promo eq 'spring01'}">
<c:set var="user" value="${(item_price-(item_price*discount))}" scope="session" />
</c:if>
<c:out value="${sessionScope.user}" />
So basically, when i click apply code, it stores the new value of the price in a variable, that i want to call on the "final purchase page"

Checked radion button based on model variable + Spring forms + JSP

I have model called cuntry.java with variable lang .I want to check the radio button option in JSP based on the lang. It could be 'EN' or 'FR'. when i wrote below code, throwing error in JSP page.
Code:
<form:radiobutton path="lang" id="language" value="FR" name="radios" <c:if ${cuntry.lang == 'FR' ? 'checked="checked"' : '' } /> ></form:radiobutton>
Error : Unterminated <form:radiobutton tag
There are multiple errors in the form tag. When using Spring tags, you may not specify id and name attributes. They will be generated automatically from path variable. Tag <c:if ... /> inside another tag <form:radiobutton ... /> is not allowed. Remove end tag as well. This is the right way
<form:radiobutton path="lang" value="FR" checked="${cuntry.lang == 'FR' ? 'checked' : '' }" />
If the value of cuntry.lang is FR, the Spring tag will result the following HTML (after compiling)
<input id="lang" name="lang" checked="checked" type="radio" value="FR" />
otherwise
<input id="lang" name="lang" type="radio" value="FR" />
If you absolutely need attributes id = language and name = radios, you can not use Spring tag .
You will find a good example here.
<c:if> tag works on tags rather than attributes. As a result, your html syntax becomes invalid.
I haven't done jsp for quite a while. this should more or less work:
<form:radiobutton checked="${cuntry.lang == 'FR' ? 'checked':''}" path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
If you insist on using <c:if> tag for this, you can do this:
<c:if test="${cuntry.lang == 'FR'}">
<form:radiobutton checked="checked" path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
</c:if>
<c:if test="${cuntry.lang != 'FR'}">
<form:radiobutton path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
</c:if>
You can also use <c:choose>,<c:when>,<c:otherwise> tags to solve this problem.
Obviously the first solution looks better.

JSTL: html snippet depending on property being null or not. (with not empty)

<c:forEach items="${list }" var="element">
<c:if test='${not empty element.radioelement}'>
<input type="radio" name="${element.name }" id="${element.id }" >
<label for="${element.id }"></label>
</c:if>
<c:if test='${not empty element.divelement}'>
<div> </div>
</c:if>
<c:if test='${not empty element.breakelement}'>
<br />
</c:if>
</c:forEach>
This is the jstl code I'm trying to find out if it works.
The list is filled with the objects of the class "tyle" which I made myself. These contain the following properties:
private String radioelement, divelement, breakelement, name, id;
I made sure they all have setters and getters.
I'm trying to make it work so that depending on which property isn't null, that piece of html will be written. (a radiobutton incase the string radioelement isn't null and so forth untill the list ends)
I'm wondering if this will work this way cus if it does I'll probably have a logical fault in my code. As it is now it only registers the breaks and makes any divelement also into a radio.

passing jslt value to javascript

I trying to a pass a jslt value to javascript but the value is not getting rendered.
<c:forEach items="${requestScope.P.Releases}" var="pr" varStatus="status"> <a href=javascript:popPR('${pr.url}') class="linkPR">
<c:out value="${pr.title}" escapeXml='false' /></a>
<c:foreach>
if directly type the pr.url value the popup window gets opened but if i pass the through jstl it does not call the popup.
Can anyone please suggest how to fix it.
Thanks
Try using <c:out> to output the url :
<c:forEach items="${requestScope.P.Releases}" var="pr" varStatus="status">
<a href=javascript:popPR('<c:out value="${pr.url}"/>') class="linkPR">
<c:out value="${pr.title}" escapeXml='false' /></a>
<c:foreach>

How to get index in jstl display table

I have display table like below
<display:table name="sample" id="sample" class="display-table" style="width:100%;">
</display:table>
Now I want to get index of loop like for first element 0 for second 1 and 2,3,4... go on. How it is possible with display table.
For just reference we can do like this in JSTL forEach loop with help of varStatus variable like below
<c:forEach items="${sample}" var="clm" varStatus="status">
${status.index}
</c:forEach>
So is this possible with display table?
display table tag implicitly exposes row number named id_rowNum where id is specified in display table tag.
In your scenario:
<display:table name="sample" id="sample" class="display-table" style="width:100%;">
<display:column title="Row Number" >
<c:out value="${sample_rowNum - 1}"/>
</display:column>
...
</display:table>
Also make sure to include core tag as:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
You can find more information about implicit objects of display table tag here.

Resources