java servlet 输出一个list 到jsp页面 循环在table中

高手们哪位知道,java servlet 输出一个list 到jsp页面 循环在table中?

public List find_meiricq_smc(String sql) {
Connection con = DbConnection_tcc.getConnection();
sql = "SELECT xuehao, name,kahao FROM ICCO.dbo.V_xsdaoru ";
List<String> lis = new ArrayList<String>();
int id = 0;
try {
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
lis.add(rs.getString(1));
lis.add(rs.getString(2));
lis.add(rs.getString(3));
lis.add("");
}
} catch (Exception e) {
Log.log(this, "queryUser:" + e);
e.printStackTrace();
} finally {
ConnTools.close(con);
}
return lis;

}
}
java Struts + Spring +servlet 我从执行这个sql 返回一个lis 而我在Action层 获取list 输出list到jsp页面
List list = service.ussyzbt(sql);
try {
request.setAttribute("list", list);

request.getRequestDispatcher("table-jsp").forward(request,response);

} catch (Exception e) {

e.printStackTrace();
}}
而我在jsp页面 这样获取
ArrayList list = (ArrayList)request.getAttribute("list");

我要怎么遍历这个list 循环在一个table中
最新回答
;單人旁┍

2024-04-24 09:00:02

通过jstl的<c:foreach>标签进行遍历,简单例子如下:
<c:if test="${!empty xxxList}">
<c:forEach items="${xxxList}" var="o" varStatus="st">
<tr>
<td align="center">${o.merchantName }</td>
<td align="center">${o.consignee }</td>
<td align="center">${o.orderAmount }</td>
</tr>
</c:forEach>
</c:if>

有问题欢迎提问,满意请采纳,谢谢!
烂命

2024-04-24 12:45:58

<table width="60%" border="1">
<tr>
<th>学号</th>

<th>姓名</th>

</tr>
<%

ArrayList list = (ArrayList)request.getAttribute("list");

for(int i=0;i<list.size();i++){
if(i%3==0){
out.println("<tr>");

}

if(i%3==2){
out.println("</tr>");

}else{
out.println("<td>"+list.get(i)+"</td>");
}
}
%>
</table>