案例需求:定义一个javaBean叫XdlUser,有四个字段int id、String name、int age、double salary。写一个jsp页面,在页面中构建一个列表对象,里面存放几个XdlUser类型的对象。
案例实现:
● XdlUser类
package com.xdl.bean;
public class XdlUser {
public XdlUser(int id, String name, int age, double salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public XdlUser() {
super();
}
@Override
public String toString() {
return "XdlUser [id=" + id + ", name=" + name
+ ", age=" + age + ", salary=" + salary + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
private int id;
private String name;
private int age;
private double salary;
}
● JSP代码
<%@page import="java.util.ArrayList"%>
<%@page import="com.xdl.bean.XdlUser"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<style type="text/css">
table {
width:600px;
text-align: center;
border-collapse: collapse;
}
table,td{
border:1px solid red;
}
</style>
</head>
<body>
<%
List<XdlUser> datas = new ArrayList<XdlUser>();
XdlUser usera = new XdlUser(1,"杜兰特",28,2400);
XdlUser userb = new XdlUser(2,"詹姆斯",32,2600);
XdlUser userc = new XdlUser(3,"库里",29,1800);
XdlUser userd = new XdlUser(4,"莱昂纳德",28,2000);
datas.add(usera);
datas.add(userb);
datas.add(userc);
datas.add(userd);
%>
<table>
<tr> <td>ID</td> <td>NAME</td> <td> AGE</td> <td> SALARY</td> </tr>
<%
for(int i=0;i<datas.size();i++){
XdlUser user = datas.get(i);
%>
<tr> <td><%=user.getId() %></td>
<td><%=user.getName()%></td>
<td> <%=user.getAge() %></td>
<td> <%=user.getSalary() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
原文地址:https://blog.51cto.com/14311187/2389779
时间: 2024-09-29 18:49:12