2017-03-22 80 views
-1

该方法的输出是一个字符串的ArrayList内容显示数组列表(java类方法)的MVC弹簧应用

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

return priceP; 
} } 

查阅(其包含在所述方法displayPrice类testh是使用Eclipse已经工作)我想显示的ArrayList在表中的内容,如下

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

,但我没有错误,但在运行它,我得到SystemError enter image description here
有没有更好的方式来做到这一点 预先感谢您

回答

0

我会建议使用JSTL库的迭代和循环的列表,而不是scriplet。

确保您的列表在页面,请求,会话和应用程序属性中可用,然后使用下面的代码片段将其打印到您的jsp中。

随着JSTL

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

,但如果你想仍然scriplet何去何从是你的选择。

选项1:循环您的列表(循环)

上选项2:使用在列表迭代器(while循环)

这是实现与选项1

随着scriplets(不推荐);

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

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

      } %> 
+0

谢谢你,我已经尝试过使用'c:foreach',但问题来了是在另一个类java(它包含java类(testh))的输出,所以我得到的方法使用scriplet,然后我使用'c:foreach'迭代,但它没有奏效,请注意输出displayPrice是一个ArrayList,所有我需要的是将其输出存储到一个arrayList然后迭代使用JSTL任何建议谢谢 –

+0

你是什么意思“是在另一个类java的输出,你打算说testh是一个内部类 – mhasan

+0

testh是在Eclipse IDE中工作得很好的类java,但是我需要在这个java web应用程序中使用它,我已将它清除了一点 –

0

步骤1:列表添加到您的春季model map作为属性

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

步骤2:您需要使用JSTL core标签

<%@ 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> 
+0

谢谢,但我没有使用Controller,testh是一个类java包含'displayPrice'方法,它返回一个ArrayList,我想在'c:foreach'中使用它 –

+0

你没有显示你的程序的流程。其实质是,您需要将带有价格的数组列表添加到请求参数中,以便JSTL核心标签可以解析它。如果你使用Spring-MVC,你通常通过ModelMap机制来完成。 – VHS