Values โ€‹โ€‹not displayed for expression language

I have a web application that displays movie schedule details (extracted from a MySQL database) when a user clicks on a movie poster.

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> 

QUEST I ON:
enter image description here
It adds the desired number of rows to the schedule table (based on the available sessions in the database), but the values โ€‹โ€‹in EL are not displayed.

The println () test in servlets appropriately receives array values โ€‹โ€‹and indexes of hard-coded arrays for table data ( schedule.malls[0] instead of ctr ) works as it should. Why are values โ€‹โ€‹not displayed when placed in a for loop?

+5
source share
3 answers

The problem is that ctr not an implicit object and is not located in any of the areas (query, session, etc.), so it is not included in the scope of EL expressions.

To fix this, you have basically two options:

OPTION No. 1 (outdated)

Use scripts (remember to import the Schedule class at the beginning of your 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> <% } %> 

OPTION number 2 (politically correct)

You will need to reorganize this Schedulle class and use JSLT tags, for example:

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

Remember to declare taglib at the beginning of your JSP:

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

I have not tested this, since I have no way to debug JSP right now, this is for you to get an idea of โ€‹โ€‹your options.

+2
source

This is basically morgano OPTION # 1 . He correctly pointed out that EL cannot read the ctr that I declared in the script, so his answer is my accepted answer.

This is just to show how I went along the path of Option No. 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
source

For each row item value displayed, add the <c:out> . Check out the following example below:

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

Happy coding

-1
source

Source: https://habr.com/ru/post/1204787/


All Articles