JSP第二次作业
作业:利用Java程序片,用for二重循环输出一个3行5列的table
考察JSP程序片和HTML代码结合使用
1.Person类
package com.example.zuoye; public class Person { private int age; private String name; private int phone; private String sex; public Person() { } public Person(int age, String name, int phone, String sex) { this.age = age; this.name = name; this.phone = phone; this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPhone() { return phone; } public void setPhone(int phone) { this.phone = phone; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Person{" + "age=" + age + ", name='" + name + '\'' + ", phone=" + phone + ", sex='" + sex + '\'' + '}'; } }
2.JSP代码
<%@ page import="com.example.zuoye.Person" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <title>JSP - Hello World</title> <style> table{ border: 1px red solid; width: 600px; border-collapse: collapse; } td,th{ border: 1px red solid; } </style> </head> <body> <% List<Person> list=new ArrayList<>(); for (int i=0;i<2;i++){ int t=18+i; list.add(new Person(t+8,"小迷糊",188845+t,"女生女生")); } %> <table> <tr> <td>年龄</td> <td>名字</td> <td>性别</td> <td>手机号</td> <td>操作</td> </tr> <% for (Person person : list) {%> <tr> <td><%=person.getAge()%></td> <td><%=person.getName()%></td> <td><%=person.getSex()%></td> <td><%=person.getPhone()%></td> <td>删除、删除</td> </tr> <%}%> </table> </body> </html>