-1

The output of the method is an Arraylist of strings (The class testh which contained the method displayPrice is already worked using Eclipse )

package classe_j;
public class testh {
public static ArrayList<String> displayPrice(String inputFileName, String categorie) {

return priceP; 
} }

Now i want to display the content of the ArrayList in table as the following

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="classe_j.*"%>
..........
<th><%=testh.displayPrice() %></th>

but i got no errors but when run it i got SystemError enter image description here
is there any better manner to do this Thank you in advance

B B
  • 50
  • 6

2 Answers2

0

I would recommend using JSTL library for iterating and looping on list instead of scriplet.

Make sure your list is available in page, request, session and application attributes and then print it in your jsp with below code snippet.

With JSTL

<c:forEach items="${list}" var="item">
    ${item}<br>
</c:forEach>

But in case you want to still go with scriplet here are your options.

Option 1 : looping on your list(for loop)

Option 2: using Iterator on list (while loop)

Here is implementation with option 1.

With scriplets (not recommended);

<% 
List<String> list = testh.displayPrice();
for (int i=0;i<list.size();i++)
          {

              out.println(list.get(i));

          } %>
mhasan
  • 3,703
  • 1
  • 18
  • 37
  • Thank you , i already tried to use `c:foreach` but the problem is the output of is in another class java(which contained the java class (testh), so i get the the method using a scriplet then i iterated using the `c:foreach` but it didn't worked , note the the output of displayPrice is an ArrayList all i need is store its output to an arrayList then iterate using JSTL any suggestion thank you – B B Mar 22 '17 at 14:38
  • What do you mean by "output of is in another class java, are you intending to say testh is an inner class – mhasan Mar 22 '17 at 14:40
  • testh is class java which is worked fine in the Eclipse IDE but i need to use it in this java web application that i made it clear a little bit – B B Mar 22 '17 at 14:46
  • Are you using any Servlet on your application? – mhasan Mar 22 '17 at 14:47
  • Only thing to use jstl library as I mentioned in my answer is to make sure you set/add you list or whatever objects you want to refer in your jsp in page/request/application scope ... that's it – mhasan Mar 22 '17 at 14:49
  • not for the moment there is no servlet , ok thank you – B B Mar 22 '17 at 15:05
  • @YassineBadri Do you mean: when you include the c:foeEach tags the interpreter marks them unknown tags? If you use namespaces, you have to define them. Link the namespace to the desired library. The JSTL core 1.1/1.2 library namespace definition is : <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> – The Bitman Mar 23 '17 at 09:27
0

Step 1: Add the list to your Spring model map as an attribute

modelMap.addAttribute("prices", testh.displayPrice("file", "category"));

Step 2: You need to use JSTL core tag

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<table>
   <tr>
      <th>Col1</th>
      <th>Col2</th>
      <!-- rest of you columns -->
   </tr>

  <c:forEach items="${prices}" var="list">
     <tr>
         <td>${list.commodity}</td>
         <td>${list.old_price} - ${list.new_price}</td> // you can add values in one column
         <!-- rest of you columns data-->
     </tr>
   </c:forEach>

 </table> 
VHS
  • 9,534
  • 3
  • 19
  • 43
  • Thank you but i m not using Controller , the testh is a class java contained the `displayPrice` method which returned an ArrayList that i want to use it in `c:foreach` – B B Mar 22 '17 at 14:49
  • You haven't shown the flow of your program. The essence is that you need to add the array list with prices to the request param so that the JSTL core tag can parse it. If you are using Spring-MVC, you typically do it through the ModelMap mechanism. – VHS Mar 22 '17 at 14:53