Reputation: 13395
I've got a class LineGroup
public class LineGroup {
private List<String> lines = null;
public LineGroup() {
lines = new ArrayList<String>();
}
public void addToList(String line) {
lines.add(line);
}
public void clear() {
lines.clear();
}
public List<String> getLines(){
return lines;
}
public String getLine(int i) {
return lines.get(i);
}
public int getLength() {
return lines.size();
}
}
In my servlet I fill it with data, set it as attribute and return to main
page
request.setAttribute("list", lg);
request.getRequestDispatcher("/main.jsp").forward(request, response);
On main page I want to read all the data from the list and put it into select
options. However it doesn't work like this
<body>
<jsp:useBean id="list" class="beans.LineGroup" scope="request"></jsp:useBean>
<form action="SevenServlet" method="POST">
ПЕРВАЯ СТРОКА:<input name="from" type="text"/><br>
ВТОРАЯ СТРОКА:<input name="to" type="text"/><br>
<input type="submit" name="send" value="УДАЛИТЬ"/><br>
<select name="deleted" disabled>
<jstl:set var="end" value="${list.getLength}"/>
<jstl:set var="begin" value="0"/>
<jstl:forEach var="i" begin="begin" end="end" step="1">
<option value="${i}">${list.getLine(i)}</option>
</jstl:forEach>
</select>
</form>
</body>
What is the correct way to get data from list
and put it into select
options?
Upvotes: 1
Views: 1801
Reputation: 691635
You're making it much more difficult that it should be.
First, you don't need to use jsp:useBean. The JSP EL uses reflection, and looks for the beans it uses in request attributes.
And you don't need to specify the begin and end of the iteration. But you must iterate over an Iterable, and your bean is not. The list it wraps is iterable though. So you just need:
<body>
<form action="SevenServlet" method="POST">
ПЕРВАЯ СТРОКА:<input name="from" type="text"/><br>
ВТОРАЯ СТРОКА:<input name="to" type="text"/><br>
<input type="submit" name="send" value="УДАЛИТЬ"/><br>
<select name="deleted" disabled>
<c:forEach var="line" items="${list.lines}" varStatus="status">
<option value="${status.index}">${line}</option>
</c:forEach>
</select>
</form>
</body>
Note that I used the standard c
prefix for the JSTL core library. Using standard names helps for redability and maintainability.
Upvotes: 5
Reputation: 2880
Try this:
<select name="deleted" disabled>
<jstl:forEach items="${list.lines}" varStatus="line">
<option value="${line.index}">${line.current}</option>
</jstl:forEach>
</select>
Upvotes: 0