0

I have this code and I have read somewhere nowadays scriplets are highly deprecated and discouraged. I'd like to use JSTL for the following code but I have some issues to understand how JSTL and EL work. Do you have any suggestion, guides, how-to?

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*, ejb.EsempioEntity" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%
    List<EsempioEntity> list = (List<EsempioEntity>)request.getAttribute("list");
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>View</title>
    </head>
    <body>
        <%
            if(list!=null)
            {
                for(EsempioEntity ee:list)
                {
                    out.print("<p>"+ee.toString()+"</p>");
                }
            }
            else
            {
                out.print("<p>Nessun dato!</p>");
            }
        %>
    </body>
</html>
Mazzy
  • 13,354
  • 43
  • 126
  • 207

2 Answers2

1

The core of it is going to look something like this:

<c:if test="${empty list}">
  <p>Nessum dato!</p>
</c:if>

<c:forEach items="${list}" var="row">
  <p><c:out value="${row}" /></p>
</c:forEach>

I recommend the resources found in the JSTL info for additional information.

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
1

Please have a look at below JSTL.

Sample code:

<c:if test="${list!=null }">
    <c:forEach items="${list}" var="item">
        <p>${item}</p>
    </c:forEach>
</c:if>
<c:if test="${list==null }">
    <p>Nessun dato!</p>
</c:if>

OR try

<c:choose>
    <c:when test="${list!=null }">
        <c:forEach items="${list}" var="item">
             <p>${item}</p>
        </c:forEach>
    </c:when>
    <c:otherwise>
        <p>Nessun dato!</p>
    </c:otherwise>
</c:choose>
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Have a look at [How to access environment variables from JSP page](http://stackoverflow.com/questions/23704812/how-to-access-environment-variables-from-jsp-page/23705004#23705004) for more info about the scope. – Braj May 17 '14 at 22:06