2017-03-22 84 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