如何使用Java方法在jsp中打印arraylist [英] how to Print arraylist in jsp using java method
本文介绍了如何使用Java方法在jsp中打印arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是Java类:
package com.example;
import java.util.ArrayList;
public class sample {
public void print() {
ArrayList<String> l = new ArrayList<String>();
l.add("a");
l.add("b");
}
}
这是我的Jsp页面:
<%@page import="java.awt.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.example.sample"%>
<%@page import="java.util.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
sample s = new sample();
%>
</body>
</html>
我不知道如何调用打印方法,如何在jsp页面中打印列表,以便在jsp页面列表中打印数据,请帮助我实现我正在学习的方法.
I dont know how to call print merhod and how Print list in jsp page so that i print data in jsp page list please help me how i will Implement i am learning this.
推荐答案
首先从打印方法返回列表
First return a list from print method
public List<String> print() {
ArrayList<String> l = new ArrayList<String>();
l.add("a");
l.add("b");
return l;
}
不要使用scriplets,请使用JSTL
Don't use scriplets, Use JSTL
<%
sample s = new sample(); //not recommended.Pass this object from servlet
List<String> list = s.print();
%>
与该打印件一起打印到jsp
with that print where ever you want to print in jsp
使用JSTL
<c:forEach items="${list}" var="item">
${item}<br>
</c:forEach>
带有Scriplet(不推荐);
<% for (int i=0;i<list.size();i++)
{
out.println(list.get(i));
} %>
借助iterator
;
With help of iterator
;
<% Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
out.println(<iterator.next());
}
%>
旁注:未经测试,请确保已在 JSP 中导入所有使用的类.
Side note:Not tested ,and make sure that you have imported all used classes in JSP.
这篇关于如何使用Java方法在jsp中打印arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文