我有一个网络应用程序,当用户点击电影海报时,它会显示电影时间表详细信息(从 MySQL 数据库中检索)。
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
}
小服务程序:
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>
问题:
它正在将所需数量的行添加到计划表(基于数据库中的可用放映时间),但 EL 中的值没有出现。
测试 Servlet 中的 println() 是否正确获取每个表数据的数组值和硬编码数组索引(schedule.mals[0]
而不是 ctr
)应该是。
为什么值放在 for 循环中时不显示?
最佳答案
问题是 ctr
不是隐式对象,也不在任何范围(请求、 session 等)中,因此它不在 EL 表达式的范围内。
要修复它,您基本上有两种选择:
选项 #1(已弃用)
使用 scriptlet(不要忘记在 JSP 的开头导入类 Schedule
):
<%
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>
<% } %>
选项 #2(政治正确的选项)
您需要重构 Schedule
类并使用 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>
不要忘记在 JSP 的开头声明标签库:
<% taglib prefix="c" uri="http://java.sun.com/jsp/jslt/core" %>
我还没有对此进行测试,因为我现在无法调试 JSP,这是为了让您了解您的选择。
关于java - 不显示表达式语言的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26396129/