Veljko Markovic Veljko Markovic - 16 days ago 5
Java Question

JSP foreach output

I have a problem with foreach output in JSP netbeans.

<tr>
<td>Dan od:</td>
<td>
<select name="danOd">
<c:forEach begin="1" end="31" step="1" var="i">
<option value="${i}">${i}</option>
</c:forEach>
</select>
</td>
<td>
<select name="mesecOd">
<c:forEach var="i" begin="1" end="12" step="1">
<option value="${i}">${i}</option>
</c:forEach>
</select>
</td>
<td>
<select name="godinaOd">
<c:forEach begin=2010" end="2020" step="1" var="i">
<option value="${i}">${i}</option>
</c:forEach>
</select>
</td>
</tr>


This is part of the JSP page I am trying to output. I want to output 1-31 in first option, 1-12 in second and 2010-2020 in third, but this is what I get and I dont understand why.

I tried to find the answer but nothing helped, everything seems right to me. Here is the link of the image of my output.

https://i.stack.imgur.com/166c4.png

Thanks in advance

inspect element pic

Sourced file to browser:

<select name="danOd">
<c:forEach begin="1" end="31" step="1" var="i">
<option value=""></option>
</c:forEach>
</select>


In the sourced file there is nothing in the value of the option, why? And thanks for this

Answer

There is a typo in your last select

<c:forEach begin=2010" end="2020" step="1" var="i">
    <option value="${i}">${i}</option>
</c:forEach>

You are missing one "

<c:forEach begin="2010" end="2020" step="1" var="i">
    <option value="${i}">${i}</option>
</c:forEach>

This will compile and work with that correction.

EDIT :

To use the Jstl core, you need to add it to the JSP page with a specific tag, the taglib. This should be at the top of the file. After the <%@page contentType... %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Without this, the server doesn't recognize the tags an skip them (see like HTML tags) and ends up in the client side.