使用jstl标签,el表达式将user信息展示在表格的练习

  • javabean中格式化日期
  • jsp用el表达式获取这个属性时,直接去掉get,小写首字母即可,成员变量里不需要定义
  • 逻辑视图的用法
    /**
     * 逻辑视图
    * */

    public String getBirStr(){
        if (birthday != null) {
            //1.格式化日期对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(birthday);
        }else {

        return "";
        }
    }
package com.heima.pojo;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Student {
    private String name;
    private int age;
    private Date birthday;

    public Student() {
    }

    public Student(String name, int age, Date birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    /**
     * 逻辑视图
    * */

    public String getBirStr(){
        if (birthday != null) {
            //1.格式化日期对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(birthday);
        }else {

        return "";
        }
    }

    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 Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="com.heima.pojo.Student" %>
<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: djy
  Date: 2022/5/5
  Time: 10:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>jstl标签,el表达式将user信息展示在表格</title>
</head>
<body>
<%
    List list = new ArrayList();
    list.add(new Student("张三", 23, new Date()));
    list.add(new Student("李四", 24, new Date()));
    list.add(new Student("王五", 25, new Date()));

    request.setAttribute("list", list);

%>

<table border="1" width="800" align="center" bgcolor="#faebd7">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>生日</th>
        <th>生日(整形后)</th>
    </tr>
    <%--数据行--%>
    <c:forEach items="${list}" var="stu" varStatus="s">
        <%--奇数行--%>
        <c:if test="${s.count % 2 != 0}">
            <tr bgcolor="#ff7f50">
                <td align="center">${s.count}</td>
                <td>${stu.name}</td>
                <td>${stu.age}</td>
                <td>${stu.birthday}</td>
                    <%--这里是get方法去了get小写首字母的属性,成员变量里不需要定义--%>
                <td>${stu.birStr}</td>
            </tr>
        </c:if>
        <%--偶数行--%>
        <c:if test="${s.count % 2 == 0}">
            <tr bgcolor="#7fffd4">
                <td align="center">${s.count}</td>
                <td>${stu.name}</td>
                <td>${stu.age}</td>
                <td>${stu.birthday}</td>
                    <%--这里是get方法去了get小写首字母的属性,成员变量里不需要定义--%>
                <td>${stu.birStr}</td>
            </tr>
        </c:if>



    </c:forEach>
</table>
</body>
</html>

访问地址:http://localhost:8080/curd/jstl/jstl4.jsp

在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值