如何在 Thymeleaf 中拆分字符串
How to split string in Thymeleaf
我有像 wange/25011.jpg|wange/25011-1.jpg
或 null 这样的图像名称,我想将它们拆分为 wange/25011.jpg
和 wange/25011-1.jpg
,如果为 null,则不拆分。我试过下面的代码,但没有工作...
<td th:if="${brickset.imageNames} != null" th:each="image : ${#strings.arraySplit(brickset.imageNames, '|')}">
<a href="#" th:href="${imageBaseUrl + image}">
<img src="#" th:src="${imageBaseUrl + image}" height="64"/>
</a>
</td>
这是我自己的回答:
<tbody id="portfolio" class="clear fix">
<tr th:each="brickset : ${bricksets}" th:alt="${brickset.description}">
<td>
<div th:unless="${brickset.imageNames == null}">
<div th:each="image,status : ${#strings.arraySplit(brickset.imageNames, '|')}">
<a href="#" th:href="${imageBaseUrl + image}" th:remove="${image} == null ? tag" th:title="${brickset.brand.name}">
<img src="#" th:src="${imageBaseUrl + image}" height="64" th:remove="${status.index} > 0 ? tag"/>
</a>
</div>
</div>
</td>
</tr>
</tbody>
在我的例子中,我有一个如下所示的字符串
字符串次数 = "Product1#100$@@@Product2#130$";
我想以 table 原始格式显示产品名称和价格,为此,我创建了这个。我用它向用户发送订单确认邮件
<tr th:each="item, count : ${#strings.arraySplit(items, '@@@')}">
<td width="75%" align="left"
style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px; padding: 15px 10px 5px 10px;"
th:text="${#strings.arraySplit(item,'#')[0] + ' ('+ (count.index + 1) +')'}">
</td>
<td width="25%" align="left"
style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px; padding: 15px 10px 5px 10px;"
th:text="${#strings.arraySplit(item,'#')[1]}">
</td>
</tr>
我有像 wange/25011.jpg|wange/25011-1.jpg
或 null 这样的图像名称,我想将它们拆分为 wange/25011.jpg
和 wange/25011-1.jpg
,如果为 null,则不拆分。我试过下面的代码,但没有工作...
<td th:if="${brickset.imageNames} != null" th:each="image : ${#strings.arraySplit(brickset.imageNames, '|')}">
<a href="#" th:href="${imageBaseUrl + image}">
<img src="#" th:src="${imageBaseUrl + image}" height="64"/>
</a>
</td>
这是我自己的回答:
<tbody id="portfolio" class="clear fix">
<tr th:each="brickset : ${bricksets}" th:alt="${brickset.description}">
<td>
<div th:unless="${brickset.imageNames == null}">
<div th:each="image,status : ${#strings.arraySplit(brickset.imageNames, '|')}">
<a href="#" th:href="${imageBaseUrl + image}" th:remove="${image} == null ? tag" th:title="${brickset.brand.name}">
<img src="#" th:src="${imageBaseUrl + image}" height="64" th:remove="${status.index} > 0 ? tag"/>
</a>
</div>
</div>
</td>
</tr>
</tbody>
在我的例子中,我有一个如下所示的字符串
字符串次数 = "Product1#100$@@@Product2#130$";
我想以 table 原始格式显示产品名称和价格,为此,我创建了这个。我用它向用户发送订单确认邮件
<tr th:each="item, count : ${#strings.arraySplit(items, '@@@')}">
<td width="75%" align="left"
style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px; padding: 15px 10px 5px 10px;"
th:text="${#strings.arraySplit(item,'#')[0] + ' ('+ (count.index + 1) +')'}">
</td>
<td width="25%" align="left"
style="font-family: Open Sans, Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 24px; padding: 15px 10px 5px 10px;"
th:text="${#strings.arraySplit(item,'#')[1]}">
</td>
</tr>