SpringBoot Thymeleaf序数 [英] SpringBoot Thymeleaf ordinal numbers

查看:68
本文介绍了SpringBoot Thymeleaf序数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一些不错的帖子,例如,其中解释了给定<$时接收序数的方法c $ c> int 。

I have read a few good posts like this one which explain the method of receiving ordinal numbers when given an int.

现在,我有一个LocalDate对象,可以使用任何 DateTimeFormat 模式。示例如下:

Now, I have a LocalDate object and I can format my dates using any of the DateTimeFormat patterns in my Thymeleaf template. Example being something like:

<strong th:text="${item.date} ? ${#temporals.format(item.date, 'dd')}"></strong>

问题:我应该怎样做?与Thymeleaf中我链接到上面的帖子类似的结果。

Question: How can I or perhaps what is the best way of achieving similar results to the post I linked to above in Thymeleaf.

我不是经验丰富的Java开发人员,所以如果您尽可能详尽地解释答案,这将对您很有帮助。

I am not an experienced Java developer so it would be very helpful if you be as thorough as you possibly can with explaining the answer.

推荐答案

在Thymeleaf模板中,您可以使用静态字段(和函数),因此您的情况如下所示:

1)与您相关的问题中的代码(我稍微修改了一下)

Inside the Thymeleaf's template you can use static fields (and functions), so in you case it will looks like that:
1) Code from the question you related (I just modified it a little bit) :

package your.packagename;
// http://code.google.com/p/guava-libraries
import static com.google.common.base.Preconditions.*;

public class YourClass {

    public static String getDayOfMonthSuffix(String num) {
        Integer n = Integer.valueOf(num == null ? "1" : num);
        checkArgument(n >= 1 && n <= 31, "illegal day of month: " + n);
        if (n >= 11 && n <= 13) {
            return "th";
        }
        switch (n % 10) {
            case 1:  return "st";
            case 2:  return "nd";
            case 3:  return "rd";
            default: return "th";
        }
    }
}

2)在视图:

<strong th:text="${#temporals.format(item.date, 'dd') + T(your.packagename.YourClass).getDayOfMonthSuffix(#temporals.format(item.date, 'dd'))}"></strong>

这篇关于SpringBoot Thymeleaf序数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆