How to get index in jstl display table - jstl

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.

Related

Does any one know how to create Empty Array In JSTL (JSP Standard Tag Library)

I am trying to create an empty in my jstl page is it possible to create an empty array and assign some value to that array in the jstl page itself .?
Your question is quite unusual. You want to do scripting without a scriplet. The only way that I could think of is to use the useBean tag to create an ArrayList. Its add method returns a boolean. That is why I used empty if tags to insert the elements. The forEach tag is not necessary. I used it to display the elements.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="myList" class="java.util.ArrayList" />
<c:if test = '${myList.add("My first element")}'>
</c:if>
<c:if test = '${myList.add("My second element")}'>
</c:if>
<c:forEach var="element" items="${myList}">
${element}
</c:forEach>
Output:
My first element My second element
You can create an array with some values using c:set:
<c:set var="array" value="${['item a','item b','item c']}" />
<c:forEach var="item" items="${array}">
<c:out value="${item}" /><br />
</c:forEach>

JSTL: c:if Not Found in Build Path

I recently migrated an ANT project to Maven. After doing all the dependecnies and getting the project error free. I see this warning on in Eclipse:
The tag handler class for "c:if" (org.apache.taglibs.standard.tag.rt.core.IfTag) was not found on the Java Build Path
I see the same warnings for c:import, c:out c:set as well.
I do have the tag:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
added in the JSP. And I have the JSTL 1.2 dependency added in the pom file. Can you tell me how to get rid of these warnings?
I had the same error. To fix it, I just added and saved the following in my pom.xml.
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.1</version>
</dependency>
In some older Eclipse versions, you may need to modify the "<c:if..." line of code, remove a character or more, save it then put it back as it was and save the file again. This in some case get rid of the warning.
My code used <c:set and <c:if as the following example. No warning displays. Perhaps, you may need to update your Eclipse. My Eclipse is IDE for Enterprise Java Developers Version: 2019-09 R (4.13.0).
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%# attribute name="normalPrice" fragment="true" %>
<%# attribute name="onSale" fragment="true" %>
<%# variable name-given="name" %>
<%# variable name-given="price" %>
<%# variable name-given="origPrice" %>
<%# variable name-given="salePrice" %>
<head>
<title>Tag Example</title>
</head>
<table border="1">
<tr>
<td>
<c:set var="name" value="Hand-held Color PDA"/>
<c:set var="price" value="$298.86"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="4-Pack 150 Watt Light Bulbs"/>
<c:set var="origPrice" value="$2.98"/>
<c:set var="salePrice" value="$2.32"/>
<jsp:invoke fragment="onSale"/>
</td>
<td>
<c:set var="name" value="Digital Cellular Phone"/>
<c:set var="price" value="$68.74"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="Baby Grand Piano"/>
<c:set var="price" value="$10,800.00"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="Luxury Car w/ Leather Seats"/>
<c:set var="origPrice" value="$23,980.00"/>
<c:set var="salePrice" value="$21,070.00"/>
<jsp:invoke fragment="onSale"/>
</td>
</tr>
</table>
<body>
<c:set var = "salary" scope = "session" value = "${2000*2}"/>
<c:if test = "${salary > 2000}">
<p>My salary is: <c:out value = "${salary}"/><p>
</c:if>
</body>
If the tag works actually fine, it is just a false negative from Eclipse. In that case you could force a new check editing the tag name and then setting it back to the right name.
So, for instance, click on your "c:if" tag, remove a single character and then re-enter it (exactly as it was before). Save it. In my case that was enough to get rid of the warning.

Output not visible jstl c:out not showing on browser

Problem: When I open page view source this data is shown on browser
Hello there jsp
<c:forEach var="student" items="2abcnullmca">
<c:out value= ""/>
</c:forEach>
<c:out value= "abc" />
And on browser it does not show c:out value in spring I have used model.addAttribute() to show data
Actually I forgot to include jstl tag thanks for looking into it but.
Here is the Tag -
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

JSTL tags don't evaluate if I import

I use dynamic include on a page:
<div class="top">
<jsp:include page="Header.jsp"/>
</div>
This is the important part in the Header.jsp:
<H4>
JSTL TAG Test: </br>
<c:if test="${sessionScope.username != null}" >
<c:out value="Hello, ${sessionScope.username}"/>
</c:if>
</h4>
The result on my main page source is:
<H4>
JSTL TAG Test: </br>
<c:if test=Swank != null >
<c:out value="Hello, Swank"/>
</c:if>
</h4>
I use this: <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> in the main page.
Does anybody know how can I use JSTL if I import it from another page?
Thanks, Zoltán
Add the same declaration
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
into your Header.jsp file.
I agree with the given answer, just with a slight modification:
<#include file="Header.jsp" %>
Notice the '%' to close the directive.
You can solve the problem by using JSP include directive
<%# include file="Header.jsp" %>
Because the content of the file given in the include directive is pasted as it is, in the place where the JSP include directive is used.
But in include action <jsp:include> At runtime, the included file will be ‘executed’ and the result content will be included with the soure JSP page.
Updated:
In JSTL condition should be written inside double quotes.
<c:if test="${sessionScope.username} != null"></c:if>
Add double quotes in your condition.

How to give c:if tag in display tag

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.

Resources