Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 表达式语言的值不显示_Java_Jsp_Servlets_For Loop_El - Fatal编程技术网

Java 表达式语言的值不显示

Java 表达式语言的值不显示,java,jsp,servlets,for-loop,el,Java,Jsp,Servlets,For Loop,El,我有一个web应用程序,当用户单击电影海报时,它会显示电影时间表的详细信息(从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; //

我有一个web应用程序,当用户单击电影海报时,它会显示电影时间表的详细信息(从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");
}
}
<%@ 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>
Servlet:

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");
}
}
<%@ 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>
JSP:

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");
}
}
<%@ 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>

...
...
查看电影时间表
... 
电影时间表
商场
电影院
价格
日期
时间
${schedule.malls[ctr]}
${schedule.cinemas[ctr]}
PHP${schedule.prices[ctr]}
${schedule.dates[ctr]}
${schedule.times[ctr]}
Q U E S T O N:

它正在将所需的行数添加到明细表中(基于数据库中可用的显示时间),但EL中的值未显示。

Servlet中的Test println()正确地获取每个表数据的数组值和硬编码数组索引(
schedule.malls[0]
,而不是
ctr
),按其应有的方式工作。


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

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

要解决此问题,您基本上有两种选择:

选项#1(不推荐)

使用Scriptlet(不要忘了在JSP开始时导入类
计划
):

不要忘了在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>
<% } %>
<% taglib prefix="c" uri="http://java.sun.com/jsp/jslt/core" %>


我还没有测试过这个,因为我现在无法调试JSP,这是为了让您了解您的选项。

这基本上是morgano的选项#1。他正确地指出,EL无法阅读我在scriptlet中声明的
ctr
,因此他的答案是我接受的答案。

这只是为了说明我是如何使用选项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");     
}
%>

...
PHP

对于要显示的每行项目值,添加
标记。签出以下示例:

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


快乐编码

您好,尽管我很想使用现代选项2,但我们在课程中尚未涉及到这一点,但非常感谢您提供了一个替代方案。投票通过并被接受为正确答案。