2014-10-16 18 views
5

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:
enter image description here
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?

Trả lời

2

Vấn đề là ctr không phải là đối tượng tiềm ẩn và không nằm trong bất kỳ phạm vi nào (yêu cầu, phiên, v.v.), vì vậy nó không nằm trong phạm vi biểu thức EL.

Để khắc phục nó, bạn có về cơ bản hai lựa chọn:

OPTION # 1 (phản đối)

Sử dụng scriptlets (đừng quên để import lớp Schedule vào đầu JSP của bạn):

<% 
Time[] times = (Time[]) session.getAttribute("times"); 

int rowCount = times.length; 

for(int ctr = 0; ctr < rowCount; ctr++) { %>   
<tr> 
    <td><%= ((Schedule)session.getAttribute("schedule")).malls[ctr] %></td> 
    <td class="cinema"><%= ((Schedule)session.getAttribute("schedule")).cinemas[ctr] %></td> 
    <td>PHP <%= ((Schedule)session.getAttribute("schedule"))..prices[ctr] %></td> 
    <td><%= ((Schedule)session.getAttribute("schedule")).dates[ctr] %></td> 
    <td><%= ((Schedule)session.getAttribute("schedule")).times[ctr] %></td> 
</tr> 
<% } %> 

OPTION # 2 (một trong những chính trị đúng đắn)

.210

Bạn sẽ cần phải refactorize rằng Schedulle lớp và sử dụng thẻ JSLT, một cái gì đó như:

<c:forEach var="rowItem" items="${rowList}" > 
<tr> 
    <td>${rowItem.mall}</td> 
    <td class="cinema">${rowItem.cinema}</td> 
    <td>PHP ${rowItem.price}</td> 
    <td>${rowItem.date}</td> 
    <td>${rowItem.time}</td> 
</tr> 
</c:forEach> 

Đừng quên khai báo taglib tại beggining của JSP của bạn:

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

tôi thiên đường 't thử nghiệm này như tôi không có cách nào để gỡ lỗi JSPs ngay bây giờ, điều này là dành cho bạn để có được một ý tưởng về các tùy chọn của bạn.

+0

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

1

Đây là cơ bản của morgano TÙY CHỌN # 1. Anh ta đã chỉ ra một cách chính xác EL không thể đọc được ctr mà tôi đã khai báo trong tập lệnh, vì vậy câu trả lời của anh ta là câu trả lời được chấp nhận của tôi.

này chỉ là để cho thấy làm thế nào tôi đã đi về phương pháp Lựa chọn # 1:

<%@ page import="com.mypackage.model.Schedule" %> 
... 
<% 
Schedule schedule = session.getAttribute("schedule") != null ? (Schedule) session.getAttribute("schedule") : null; 

if(schedule != null) { 
    int rowCount = schedule.getTimes().length; 

    for(int ctr = 0; ctr < rowCount; ctr++) { 
%> 
<tr> 
    <td><%=schedule.getMalls()[ctr] %></td> 
    <td class="cinema"><%=schedule.getCinemas()[ctr] %></td> 
    <td>PHP <%=schedule.getPrices()[ctr] %></td> 
    <td><%=schedule.getDates()[ctr] %></td> 
    <td><%=schedule.getTimes()[ctr] %></td> 
</tr> 
<% } 

} else { 
    // redirect on illegal access 
    response.sendRedirect("index.jsp");  
} 
%> 
-1

Đối với mỗi giá trị mục hàng sẽ được hiển thị thêm <c:out> tag.Checkout ví dụ sau đây:

<td><c:out value="${rowItem.mall}" /></td> 

Chúc mừng Mã hóa

Các vấn đề liên quan