您当前的位置: 首页 > 

梁云亮

暂无认证

  • 3浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JSTL 自定义标签

梁云亮 发布时间:2022-06-10 08:08:16 ,浏览量:3

说明

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')}
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.1006s