在JSP中显示对象的ArrayList。

huangapple 未分类评论48阅读模式
英文:

Display ArrayList of Objects in JSP

问题

我尝试在 JSP 页面中显示来自 ArrayList 的数据表格。

以下是我如何将 ArrayList 添加到模型中:

List<CustomType> resultList = new ArrayList<CustomType>();
resultList = getResult();

model.addAttribute("resultList", resultList);
return "DisplayResult";

这是 CustomType 类的定义:

public class CustomType implements SQLDataType {	
    private String variable1;
    private String variable2;
    private String variable3;
    private String variable4;
    private String variable5;
    private String variable6;
    private String variable7;
    private Timestamp variable8;

    // 其他代码...
}

我该如何将这个 ArrayList 显示为表格?

以下是我尝试过但似乎不起作用的方法:

<div id="resultTable">
    <table>
        <tr>
            <th>variable1</th>
            <th>variable2</th>
            <th>variable3</th>
            <th>variable4</th>
            <th>variable5</th>
            <th>variable6</th>
            <th>variable7</th>
            <th>variable8</th>
        </tr>
        <c:forEach var="result" items="${resultList}">
            <tr>
                <td>${result.variable1}</td>
                <td>${result.variable2}</td>
                <td>${result.variable3}</td>
                <td>${result.variable4}</td>
                <td>${result.variable5}</td>
                <td>${result.variable6}</td>
                <td>${result.variable7}</td>
                <td>${result.variable8}</td>
            </tr>
        </c:forEach>
    </table>
</div>
英文:

I was trying to display a table in jsp page with data from ArrayList.

Here is how I added the ArrayList to Model

List&lt;CustomType&gt; resultList = new ArrayList&lt;CustomType&gt;();
resultList = getResult();

model.addAttribute(&quot;resultList&quot;, resultList);
return &quot;DisplayResult&quot;;

Here is the CustomType:

public class CustomType implements SQLDataType {	
private String variable1;
private String variable2;
private String variable3;
private String variable4;
private String variable5;
private String variable6;
private String variable7;
private Timestamp variable8;

....
..

How can I display the ArrayList as Table ?

Here's how I've been trying this and it doesn't seem to work

&lt;div id=&quot;resultTable&quot;&gt;
        &lt;table&gt;
            &lt;tr&gt;
            &lt;th&gt;variable1&lt;/th&gt;
            &lt;th&gt;variable2&lt;/th&gt;
            &lt;th&gt;variable3&lt;/th&gt;
            &lt;th&gt;variable4&lt;/th&gt;
            &lt;th&gt;variable5&lt;/th&gt;
            &lt;th&gt;variable6&lt;/th&gt;
            &lt;th&gt;variable7&lt;/th&gt;
            &lt;th&gt;variable8&lt;/th&gt;
        &lt;/tr&gt;
        &lt;c:forEach var=&quot;result&quot; items=&quot;${resultList}&quot;&gt;
            &lt;tr&gt;
                &lt;td&gt;${result.variable1}&lt;/td&gt;
                &lt;td&gt;${result.variable2}&lt;/td&gt;
                &lt;td&gt;${result.variable3}&lt;/td&gt;
                &lt;td&gt;${result.variable4}&lt;/td&gt;
                &lt;td&gt;${result.variable5}&lt;/td&gt;
                &lt;td&gt;${result.variable6}&lt;/td&gt;
                &lt;td&gt;${result.variable7}&lt;/td&gt;
                &lt;td&gt;${result.variable8}&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/c:forEach&gt;
    &lt;/table&gt;
&lt;/div&gt;

答案1

得分: -1

public static Fruit[] getFruits(){
    Vector<Fruit> v = new Vector();
    try{
        File file = new File(Controller.path, "Fruit.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while((line = br.readLine()) != null){
            Fruit f = new Fruit(line);
            v.add(f);
        }
        br.close();
    }catch(Exception e){
        e.printStackTrace();
    }
    Fruit[] arr = new Fruit[v.size()];
    v.toArray(arr);
    return arr;
}
<%
    Fruit[] arr = (Fruit[]) request.getAttribute("labelList");
    if(arr != null){
%>
    <table border='1'>
        <% for(Fruit fr : arr){ %>
        <tr>
            <td>
                <%=fr.id%>
            </td>
            <td>
                <a href='C?cmd=Details&amp;id=<%=fr.id%>'><%=fr.nameFruit%></a>
            </td>
        </tr>
        <%}%>
    </table>
<%}%>

首先,从数据库加载数据并将其存储在列表中,然后将其发送到 JSP 页面。

英文:

java file:

public static Fruit[] getFruits(){
        Vector&lt;Fruit&gt; v = new Vector();
        try{
            File file = new File(Controller.path,&quot;Fruit.txt&quot;);
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while((line = br.readLine()) != null){
                Fruit f = new Fruit(line);
                v.add(f);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        Fruit[] arr = new Fruit[v.size()];
        v.toArray(arr);
        return arr;
    }

jsp file:


&lt;%
          Fruit[] arr = (Fruit[]) request.getAttribute(&quot;labelList&quot;);
          if(arr != null){
        %&gt;
        &lt;table border=&#39;1&#39;&gt;
            &lt;% for(Fruit fr : arr){ %&gt;
            &lt;tr&gt;
                &lt;td&gt;
                    &lt;%=fr.id%&gt;
                &lt;/td&gt;
                &lt;td&gt;
                    &lt;a href=&#39;C?cmd=Details&amp;id=&lt;%=fr.id%&gt;&#39;&gt;&lt;%=fr.nameFruit%&gt;&lt;/a&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;%}%&gt;
        &lt;/table&gt;
        &lt;%}%&gt;

First of all, Load the data from the database and store it in the list, then send it to jsp.

huangapple
  • 本文由 发表于 2020年7月23日 15:30:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/63049049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定