我在servlet中有一个Map
Map<String, Boolean> seatsModelComplete = new LinkedHashMap<>();
string
代表座位号码,Boolean
表示座位是否保留。
我在请求中设置了如下属性:
request.setAttribute("seats", seatsModelComplete);
但是我无法在JSP中使用它。具体来说,地图不会被以下foreach
循环
<c:forEach items="${requestScope.seats}" var="seats">
...
</c:forEach>
这是我的Servlet:
@WebServlet(name =“SeatsServlet”,urlPatterns =“/ seat”)
public class SeatsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<MovieDTO> movieDTOList = (List<MovieDTO>) request.getSession().getAttribute("movieDTOList");
int movie_id = Integer.parseInt(request.getParameter("movie_id"));
int hall_id = Integer.parseInt(request.getParameter("hall_id"));
int showtime_id = Integer.parseInt(request.getParameter("showtime_id"));
MovieDTO selected_movie = null;
Hall selected_hall = null;
Showtime selected_showtime = null;
for (MovieDTO m : movieDTOList) {
if (m.getId() == movie_id) {
selected_movie = m;
break;
}
}
if (selected_movie != null) {
request.setAttribute("movie_title", selected_movie.getTitle());
}
for (Hall h : selected_movie.getHallList()) {
if (h.getId() == hall_id) {
selected_hall = h;
break;
}
}
if (selected_hall != null) {
request.setAttribute("hall_id", selected_hall.getId());
}
for (Showtime s : selected_hall.getShowtimes()) {
if (s.getId() == showtime_id) {
selected_showtime = s;
break;
}
}
if (selected_showtime != null) {
request.setAttribute("showtime_time", selected_showtime.getTime());
}
List<Seat> seats = selected_showtime.getSeats();
int maxRows = (PropertyHolder.getInstance().getMaxRowsCount().toLowerCase().toCharArray()[0] - 'a') + 1;
int maxNumbers = Integer.parseInt(PropertyHolder.getInstance().getMaxNumberCount());
String aSeat;
boolean isReserved;
Map<String, Boolean> seatsModelComplete = new LinkedHashMap<>();
for (int i = 0; i < maxRows; i++) {
for (int j = 0; j < maxNumbers; j++) {
aSeat = (((char)('A' + i)) + "").concat((j + 1) + "") ;
isReserved = false;
for (Seat seat : seats) {
if (seat.getRow().concat(seat.getNumber() + "").equals(aSeat)){
isReserved = true;
}
}
seatsModelComplete.put(aSeat, isReserved);
}
}
request.setAttribute("seats", seatsModelComplete);
// request.getSession().setAttribute("seats", seatsModelComplete); map gets passed only here, not in request scope
request.getRequestDispatcher("/pages/seat_selection/seat_selection.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
答案 0 :(得分:0)
使用此:
<c:forEach items="${seats}" var="seat">
Key is ${seat.key}
Value is ${seat.value}
</c:forEach>
答案 1 :(得分:0)
你确定你初始化了吗?愚蠢的问题但是...... 此外,你可以尝试像
这样的东西<c:forEach items="${seats}" var="seat">
<option value="${seat.key}">${seat.taken}</option
</c:forEach>
编辑:你可能用jsp弄乱了一些东西,你可以随时测试你的集合是否正确初始化和/或是否在适当的范围内找到:
<c:choose>
<c:when test="${empty seats}">
Seats are empty or null.
</c:when>
<c:otherwise>
Seats are not empty or null
</c:otherwise>
</c:choose>