如何在freemarker模板中排序之前更新值

0yycz8jy  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(331)

我有以下html模板

[#list PRODUCTS?sort_by("Price_origin_type") as product]
[/#list]

我有以下例外

Exception while merging FreeMarker template with values - ?sort_by(...) failed at sequence index 2 (0-based): All key values in the sequence must be numbers, because the first key value was that. However, the key value of the current item isn't a number.

有没有办法为空值设置0?

7y4bm7vi

7y4bm7vi1#

这不会将空值替换为零,但应将其从列表中排除:

[#list PRODUCTS?filter(p -> p != "")?sort_by("Price_origin_type") as product]
[/#list]
jtjikinw

jtjikinw2#

用java代码对集合排序。

在JavaDTO类中创建一个专用的排序字段。

class ProductDto {
  private Integer priceOriginType;
  // other fields

  public int getPriceSortingIndex() {
    return Optional.ofNullable(priceOriginType).orElse(0);
  }
}

使用 products?sort_by("priceSortingIndex") 在模板中。

相关问题