采用jsp用表格的形式显示

时间:2023-02-08 23:10:02

<%@page import="cn.hncu.domain.*"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style type="text/css">
table{
border:1px solid black;
border-collapse: collapse;
width:200px;
}
td{
border:1px solid black;
}

</style>
</head>

<body>
<H2>用户信息如下</H2>
用户姓名:${user.name}<br/>
用户年龄:${user.age}<br/>

<p>车辆信息如下:</p>
<table>
<tr> <th>车名</th> <th>编号</th> <th>颜色</th> </tr>
<%
Car cs[] = ((User) request.getAttribute("user")).getCars();
for( Car c : cs ){
pageContext.setAttribute("c", c);
%>
<tr> <td> ${c.name} </td> <td>${c.id}</td> <td>${c.color} </td> </tr>
<%
}
%>
</table>

<c:if test="${!empty user.cars}">
<hr/>
<h3>JSTL出场</h3>
<table>
<tr> <th>车名</th> <th>编号</th> <th>颜色</th> </tr>
<!-- for( Car c: cars) -->
<c:forEach items="${user.cars}" var="c" varStatus="idx">
<tr> <td>${idx.count} ${c.name} </td> <td>${c.id}</td> <td>${c.color} </td> </tr>
</c:forEach>
</table>
</c:if>


</body>
</html>