Thymeleaf用户生成的内容中的十进制格式选项

huangapple go评论58阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年7月28日 17:55:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63131490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定