英文:
Thymeleaf decimal formatting options at user generated content
问题
我正在尝试在后端使用Thymeleaf解析和处理用户生成的内容。因此,我们已经完成了以下操作:
@RestController
@RequestMapping(path = "/api/v1")
@Slf4j
public class MyController {
@Autowired
private TemplateEngine templateEngine;
...
@ApiResponse(code = 200, message = "OK", response = DeliveryDTO.class)
@PostMapping(path = "/{myId}/serve")
public DeliveryDTO serve(
@PathVariable String myId
) {
... // 获取 myObj;
html = templateEngine.process(myObj.getHtml(), myObj.getContext());
这部分已经翻译完成。接下来的部分是您提到的HTML内容,我会继续翻译:
这部分已经翻译完成。接下来的部分是您提到的HTML内容,我会继续翻译:
```html
这部分已经翻译完成。接下来的部分是您提到的HTML内容,我会继续翻译:
```html
<table>
<tr th:each="item: ${ko}">
<td>
<a th:href="@{${URLBuilder.build(item)}}"><span th:text="${item.isin}"></span></a>.
</td>
<td>
<span th:text="${item.value1}"></span>
</td>
<td>
<span th:text="${item.value2}"></span>
</td>
</tr>
</table>
现在,我想要将所有出现的"item.value1"格式化为两位小数,"item.value2"格式化为三位小数。我可以将这些添加到HTML模板中,但由于这是某种用户内容,我更喜欢遍历所有出现的地方并添加一个小数格式选项。有关如何实现这一点的任何想法吗?
希望能对您有所帮助。
英文:
I am trying to parse and process a user generated content in the backend with thyme leaf.
Therefore we have done the following:
@RestController
@RequestMapping(path = "/api/v1")
@Slf4j
public class MyController {
@Autowired
private TemplateEngine templateEngine;
...
@ApiResponse(code = 200, message = "OK", response = DeliveryDTO.class)
@PostMapping(path = "/{myId}/serve")
public DeliveryDTO serve(
@PathVariable String myId
) {
... // get myObj;
html = templateEngine.process(myObj.getHtml(), myObj.getContext());
this works really fine. But now we have some parts of myObj which should get a default formatting. For example
myObj.getHtml() could deliver this:
<table>
<tr th:each="item: ${ko}">
<td>
<a th:href="@{${URLBuilder.build(item)}}"><span th:text="${item.isin}"></span></a>.
</td>
<td>
<span th:text="${item.value1}"></span>
</td>
<td>
<span th:text="${item.value2}"></span>
</td>
</tr>
</table>
Now I want all occurrences of "item.value1" to be formatted with two decimals and "item.value2" with three decimals. I could add that to the html-template, but as this is some kind of user content I would prefer to iterate over all occurrences and add a decimal formatting option.
Any ideas on how to achieve this?
Cheers Maik
答案1
得分: 1
你可以在DTO的字段上使用@NumberFormat
注解。
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#format-annotations-api
如果不行,Thymeleaf也有一些格式化选项。
英文:
You can use the @NumberFormat
annotation on the fields in your DTO.
https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#format-annotations-api
If that's not possible, thymeleaf also has some formatting options
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论