说明
JSTL默认不支持LocalDate和LocalDateTime,在JSP中自定义显示LocalDate和LocalDateTime,需要通过自定义标签实现。
第一步:创建具体进行转换的工具类- LocalDateTimeFormatter
public class LocalDateTimeFormatter {
public static String formatLocalDateTime(LocalDateTime localDateTime, String pattern) {
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}
}
- LocalDateFormatter
public class LocalDateFormatter {
public static String formatLocalDate(LocalDate localDate, String pattern) {
return localDate.format(DateTimeFormatter.ofPattern(pattern));
}
}
第二步:在WEB-INF目录下创建tld文件
1.0
Custom_Functions
http://wego.com/format
formatLocalDateTime
com.wego.formatter.LocalDateTimeFormatter
java.lang.String formatLocalDateTime(java.time.LocalDateTime, java.lang.String)
formatLocalDate
com.wego.formatter.LocalDateFormatter
java.lang.String formatLocalDate(java.time.LocalDate, java.lang.String)
第三步:测试
- 服务器端数据
request.setAttribute("localDate", LocalDate.now());
request.setAttribute("localDateTime", LocalDateTime.now());
- 页面使用
日期:${f:formatLocalDate(localDate, 'yyyy-MM*dd')}
日期时间:${f:formatLocalDateTime(localDateTime, 'yyyy-MM-dd HH:mm:ss')}