Tôi có ứng dụng web hiển thị chi tiết lịch biểu phim (được truy xuất từ cơ sở dữ liệu MySQL) khi người dùng nhấp vào áp phích phim.Giá trị không hiển thị cho ngôn ngữ biểu thức
Bean:
import java.sql.Date;
import java.sql.Time;
public class Schedule {
private String[] malls;
private Integer[] cinemas;
private Double[] prices;
private Date[] dates;
private Time[] times;
// getters and setters
}
Servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int movieId = request.getParameter("movieid") != null ? Integer.parseInt(request.getParameter("movieid")) : 0;
if(movieId != 0) {
DatabaseManipulator dm = new DatabaseManipulator();
...
// get schedule details from database
String[] malls = dm.getMallNames(movieId);
Integer[] cinemas = dm.getCinemaNumbers(movieId);
Double[] prices = dm.getMoviePrices(movieId);
Date[] dates = dm.getShowDates(movieId);
Time[] times = dm.getShowTimes(movieId);
// assemble bean objects
Schedule schedule = ScheduleAssembler.getInstance(malls, cinemas, prices, dates, times);
// returns new session if it does not exist
HttpSession session = request.getSession(true);
// bind objects to session
session.setAttribute("schedule", schedule);
session.setAttribute("times", times); // for schedule row count
// redirect to view schedule page
response.sendRedirect("view-schedule.jsp");
} else {
// redirect when servlet is illegally accessed
response.sendRedirect("index.jsp");
}
}
JSP:
<%@ page import="java.sql.*" %>
...
<body>
...
<strong>VIEW MOVIE SCHEDULE</strong>
...
<table id="schedule">
<tr><td class="titlebg" colspan="5">MOVIE SCHEDULE</td></tr>
<tr>
<td class="catbg">Mall</td>
<td class="catbg">Cinema</td>
<td class="catbg">Price</td>
<td class="catbg">Date</td>
<td class="catbg">Time</td>
</tr>
<%
Time[] times = (Time[]) session.getAttribute("times");
int rowCount = times.length;
for(int ctr = 0; ctr < rowCount; ctr++) { %>
<tr>
<td>${schedule.malls[ctr]}</td>
<td class="cinema">${schedule.cinemas[ctr]}</td>
<td>PHP ${schedule.prices[ctr]}</td>
<td>${schedule.dates[ctr]}</td>
<td>${schedule.times[ctr]}</td>
</tr>
<% } %>
</table>
</body>
Q U E S T I O N:
Nó đang thêm số hàng mong muốn vào bảng lịch biểu (dựa trên lịch chiếu có sẵn trong cơ sở dữ liệu), nhưng giá trị trong EL không xuất hiện.
Kiểm tra println() trong Servlet phù hợp nhận các giá trị mảng và chỉ số mảng được mã hóa cứng trên mỗi bảng dữ liệu (schedule.malls[0]
thay vì ctr
) hoạt động như mong muốn.
Tại sao các giá trị không hiển thị khi được đặt trong vòng lặp for?
Hi, càng nhiều càng tốt tôi sẽ muốn đi với Lựa chọn số 2 hiện đại, chúng tôi chưa chạm vào điều đó trong các bài học của chúng tôi, nhưng cảm ơn bạn rất nhiều vì đã cung cấp giải pháp thay thế. Upvoted và được chấp nhận là câu trả lời đúng. – silver