- 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) {
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
|