JSTL--练习

    科技2022-07-20  98

    黑马程序员

    JSTL–练习

    需求:在request域中有一个存有User对象的List集合。需要使用jstl+el将list集合数据展示到jsp页面的表格table中,并且要求表格居中,且表格边框为1px,宽度为500px,并且其奇数行背景颜色为#bc8f8f,偶数行背景颜色为#adff2f * 代码如下: <%@ page import="zr.web.domain.User" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.Date" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>test</title> </head> <body> <% List list =new ArrayList(); list.add(new User("张三",23,new Date())); list.add(new User("李四",22,new Date())); list.add(new User("王五",24,new Date())); request.setAttribute("list",list); %> <table border="1" align="center" width="500"> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>生日</th> </tr> <%-- 数据行--%> <%-- 变色 奇数行背景色为#bc8f8f 偶数行背景色为#adff2f--%> <c:forEach items="${list}" var="user" varStatus="s"> <c:if test="${s.count % 2 == 0}"> <tr bgcolor="#adff2f"> <td>${s.count}</td> <td>${user.name}</td> <td>${user.age}</td> <td>${user.birStr}</td> </tr> </c:if> <c:if test="${s.count % 2 != 0}"> <tr bgcolor="#bc8f8f"> <td>${s.count}</td> <td>${user.name}</td> <td>${user.age}</td> <td>${user.birStr}</td> </tr> </c:if> </c:forEach> </table> </body> </html> package zr.web.domain; import java.text.SimpleDateFormat; import java.util.Date; public class User { private String name; private int age; private Date birthdayDate; public User(String name, int age, Date birthdayDate) { this.name = name; this.age = age; this.birthdayDate = birthdayDate; } public User() { } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setBirthdayDate(Date birthdayDate) { this.birthdayDate = birthdayDate; } public String getName() { return name; } public int getAge() { return age; } public Date getBirthdayDate() { return birthdayDate; } /** * 逻辑视图 * @return */ public String getBirStr(){ //格式化日期 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //返回日期字符串 return sdf.format(birthdayDate); } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", birthdayDate=" + birthdayDate + '}'; } }
    Processed: 0.011, SQL: 8