不显示表达式语言的值

sil*_*ver 5 java jsp for-loop servlets el

我有一个Web应用程序,当用户点击电影海报时,它会显示电影时间表详细信息(从MySQL数据库中检索).

豆:

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
}
Run Code Online (Sandbox Code Playgroud)

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");
}
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

题:
在此输入图像描述
它将所需的行数添加到计划表中(基于数据库中的可用放映时间),但EL中的值未显示.

测试Servlet中的println()是否适当地获取每个表数据(schedule.malls[0]而不是ctr)的数组值和硬编码数组索引.

放置在for循环中时,为什么值不显示?

mor*_*ano 2

问题在于它ctr不是隐式对象,也不在任何范围(请求、会话等)中,因此它不在 EL 表达式的范围中。

要修复它,您基本上有两种选择:

选项#1(已弃用)

Schedule使用 scriptlet(不要忘记在 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>
<% } %>
Run Code Online (Sandbox Code Playgroud)

选项#2(政治正确的选项)

您需要重构该类Schedulle并使用 JSLT 标记,例如:

<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>
Run Code Online (Sandbox Code Playgroud)

不要忘记在 JSP 的开头声明 taglib:

<% taglib prefix="c" uri="http://java.sun.com/jsp/jslt/core" %>
Run Code Online (Sandbox Code Playgroud)

我还没有对此进行测试,因为我现在无法调试 JSP,这是为了让您了解您的选择。