简体   繁体   English

使用 Thymeleaf 和 Spring Boot 转换器列出 JSON 字符串

[英]List to JSON string with Thymeleaf and Spring Boot converter

I'm working on a service that produces HTML pages via Thymeleaf templates.我正在开发一项通过 Thymeleaf 模板生成 HTML 页面的服务。 In one of those templates I would like to have an HTML attribute as a JSON string.在其中一个模板中,我希望将 HTML 属性作为 JSON 字符串。 The related Object in my context is an ArrayList<String> .我上下文中的相关对象是ArrayList<String> Without doing anything the output would be "[item1, item2]" but I want "["random","stuff"]" .如果不做任何事情,输出将是"[item1, item2]"但我想要"["random","stuff"]"

I've read about Converter and Formatter and I thought that was the way to go.我已经阅读了关于ConverterFormatter ,我认为这是要走的路。 But I cannot get my conversion system to work.但我无法让我的转换系统工作。

Here is my custom Converter :这是我的自定义Converter

public class ListConverter implements Converter(ArrayList<String>, String {
  public String convert (ArrayList<String> source) {
    return new JSONArray(source).toString();
  }
}

The main class looks like主类看起来像

@SpringBootApplication
public class TheApplication extends WebMvcConfigurerAdapter {

  public static void main(String[] args) {
    SpringApplication.run(PageServiceApplication.class, args);
  }

  @Bean
  public ListConverter listConverter() {
    return new ListConverter();
  }

  @Override
  public void addFormatters(FormatterRegistry registry) {
    registry.addConverter( listConverter() );
  }
}

Finally the Thymeleaf template looks like最后 Thymeleaf 模板看起来像

<some-webcomponent xmlns:th="http://www.thymeleaf.org"
    th:attrappend="tags=${data.tags} ...">
</some-webcomponent>

So tags is my ArrayList<String> .所以tags是我的ArrayList<String> I have also tried forcing the conversion with ${{data.tags}} or with ${#conversions.convert(data.tags, 'String'} but the only this is does is turn "[item1, item2]" to "item1,item2" .我还尝试使用${{data.tags}}${#conversions.convert(data.tags, 'String'}但唯一的做法是将"[item1, item2]"变为"item1,item2"

Doing tags=${new org.json.JSONArray(data.tags)} works but I could like to have that elsewhere and probably not only for ArrayList<String> .tags=${new org.json.JSONArray(data.tags)}工作,但我想在其他地方有它,可能不仅仅是ArrayList<String>

So my questions are :所以我的问题是:

  • is this possible at all ?这可能吗?
  • are Converter the way to go ? Converter是要走的路吗?
  • what am I missing with the configuration ?我缺少什么配置?

Thank you.谢谢你。

For whatever reason, it works using List instead of ArrayList.无论出于何种原因,它都使用 List 而不是 ArrayList。 Also, I would get rid of the addFormatters method.另外,我会摆脱 addFormatters 方法。 You just need the bean declaration.您只需要 bean 声明。

Spring Boot:弹簧靴:

@SpringBootApplication
public class TheApplication extends WebMvcConfigurerAdapter {

  public static void main(String[] args) {
    SpringApplication.run(PageServiceApplication.class, args);
  }

  @Bean
  public Converter<List<String>, String> converter() {
    return new Converter<List<String>, String>() {
      public String convert(List<String> source) {
        return new JSONArray(source).toString();
      }
    };
  }
}

Thymeleaf (double bracket for tags) Thymeleaf(标签的双括号)

<some-webcomponent xmlns:th="http://www.thymeleaf.org"
    th:attrappend="tags=${{data.tags}} ...">
</some-webcomponent>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM