2014-10-16 31 views
5

Mam aplikację internetową, która wyświetla szczegóły harmonogramu filmu (pobrane z bazy danych MySQL), gdy użytkownik kliknie plakat filmowy.Wartości niewyświetlane dla języka wyrażeń

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> 

Jest to dodanie wymaganej liczby wierszy do tabeli harmonogramu (w oparciu o dostępne repozytorium w bazie danych), ale wartości w EL nie pojawiają się.

Test println() w Servletu odpowiednio pobiera wartości tablicowe i zakodowane indeksy tablicowe na dane tabeli (schedule.malls[0] zamiast ctr) działa tak, jak być powinno.

Dlaczego wartości nie są wyświetlane po umieszczeniu w pętli for?

Odpowiedz

2

Problem polega na tym, że ctr nie jest obiektem niejawnym i nie znajduje się w żadnym z zakresów (żądanie, sesja itp.), Więc nie należy do zakresu wyrażeń EL.

Aby to naprawić trzeba w zasadzie dwie opcje:

OPCJA # 1 (przestarzałe)

Stosować scriptlets (nie zapomnij zaimportować klasę Schedule na początku swojej JSP):

<% 
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> 
<% } %> 

opcji # 2 (politycznych poprawne)

Musisz refactorize że Schedulle klasy i używać znaczników JSLT, coś jak:

<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> 

Nie zapomnij zadeklarować TagLib na początku działalności swojej JSP:

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

I haven Nie przetestowałem tego, ponieważ nie mam teraz możliwości debugowania stron JSP, aby uzyskać informacje na temat dostępnych opcji.

+0

Cześć, jak bym chcemy iść z nowoczesną opcją nr 2, jeszcze tego nie poruszaliśmy na lekcjach, ale dziękujemy bardzo za dostarczenie alternatywy. Rewizja i akceptacja jako poprawna odpowiedź. – silver

1

Jest to w zasadzie morgano w opcji OPCJA # 1. Poprawnie wskazał, że EL nie jest w stanie odczytać ctr, którą zadeklarowałem w skrypcie, więc jego odpowiedź jest moją akceptowaną odpowiedzią.

To jest po prostu pokazać, jak poszedłem o Option # 1 podejście:

<%@ 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

dla każdej wartości przedmiotu wiersz zostać wyświetlony dodać <c:out> tag.Checkout następujący przykład poniżej:

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

Szczęśliwy Coding