【问题标题】:Spring Boot & Thymeleaf with XML Templates带有 XML 模板的 Spring Boot 和 Thymeleaf
【发布时间】:2015-02-27 21:48:44
【问题描述】:

我有一个带有控制器的 Spring Boot 应用程序,该控制器返回 ModelAndView 和 Thymeleaf 以呈现模板,其中模板位于 /src/main/resources/templates/*.html 中

这很好用,但是如何配置 Spring 和/或 Thymeleaf 以查找 xml 文件而不是 html?

如果有帮助,我将使用 Gradle 和 org.springframework.boot:spring-boot-starter-web 依赖项进行设置。我目前正在使用带有 main 方法的类运行服务器。

【问题讨论】:

    标签: spring-mvc configuration spring-boot thymeleaf


    【解决方案1】:

    在为 viewResolver 和相关事物尝试各种 bean defs 并失败后,我终于通过更改我的 application.yaml 文件来完成这项工作:

    spring:
      thymeleaf:
        suffix: .xml
        content-type: text/xml
    

    对于稍后阅读本文的人,您可以对 application.properties 文件执行类似操作(使用点符号代替 yaml 缩进)。

    【讨论】:

    • content-type 在spring.thymeleaf.servlet.content-type
    【解决方案2】:

    这也有效:

    @Configuration
    public class MyConfig
    {
        @Bean
        SpringResourceTemplateResolver xmlTemplateResolver(ApplicationContext appCtx) {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    
            templateResolver.setApplicationContext(appCtx);
            templateResolver.setPrefix("classpath:/templates/");
            templateResolver.setSuffix(".xml");
            templateResolver.setTemplateMode("XML");
            templateResolver.setCharacterEncoding("UTF-8");
            templateResolver.setCacheable(false);
    
            return templateResolver;
        }
    
        @Bean(name="springTemplateEngine")
        SpringTemplateEngine templateEngine(ApplicationContext appCtx) {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(xmlTemplateResolver(appCtx));
            return templateEngine;
        }
    }
    

    对于用法

    @RestController
    @RequestMapping("/v2/")
    public class MenuV2Controller {
        @Autowired
        SpringTemplateEngine springTemplateEngine;
    
        @GetMapping(value ="test",produces = {MediaType.APPLICATION_XML_VALUE})
        @ResponseBody
        public String test(){
            Map<String, String> pinfo = new HashMap<>();
            Context context = new Context();
            context.setVariable("pinfo", pinfo);
            pinfo.put("lastname", "Jordan");
            pinfo.put("firstname", "Michael");
            pinfo.put("country", "USA");
    
           String content = springTemplateEngine.process("person-details",context);
           return content;
    
      }
    }
    

    不要忘记资源/模板文件夹中的模板

    <?xml version="1.0" encoding="UTF-8"?>
    <persons >
        <person>
            <fname th:text="${pinfo['lastname']}"></fname>
            <lname th:text="${pinfo['firstname']}"></lname>
            <country th:text="${pinfo['country']}"></country>
        </person>
    </persons>
    

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 2016-01-26
      • 2017-03-08
      • 2017-06-21
      • 2022-11-14
      • 2021-09-25
      • 2016-02-05
      • 2020-03-14
      • 2018-01-26
      相关资源
      最近更新 更多