I’m very (very!) new to JSP development and I’m trying to do something that I’d do in ASP.NET in about 30 seconds but the environment is doing my head in.
So far I’ve got the following on my web page:
<body>
<jsp:useBean id="allPlayerInfo" class="Player.AllPlayers" scope="page"/>
<jsp:include page="../header.jsp"/>
Player:
<select id="drp_player">
<c:forEach var="item" items="${allPlayerInfo.items}">
<option value="${item}">${item}</option>
</c:forEach>
</select>
</body>
And the AllPlayers file looks like:
package Player;
public class AllPlayers {
public java.util.List<String> getItems() {
java.util.List<String> list = new java.util.ArrayList<String>();
list.add("PlayerName1");
list.add("PlayerName2");
list.add("PlayerName3");
return list;
}
}
I’ll (obviously?) be replacing the placeholder “PlayerNames” with a database call in the future, I just wanted to get this simple test working first.
Currently, I get back a select populated with nothing. Any ideas what I’m doing wrong?
The code posted so far looks fine, although your JSP code is incomplete. The taglibs are missing in the JSP snippet. This is often the case when you just want to show “snippets”, but in real world code you should actually declare the
<c:xxx>taglib in the top of JSP. Otherwise they will simply not be interpreted by the JSP compiler and be treated as plain text. You could easily have verified this by checking the JSP-generated HTML output by rightclick, View Source in a webbrowser. You should in this particular case not have seen the<c:forEach>plain vanilla in there.I think this is the case. At least, the symptom of seeing an empty dropdown list confirms this. Add the following to the top of your JSP:
See also: