【问题标题】:Thymeleaf and static contentThymeleaf 和静态内容
【发布时间】:2015-04-02 07:57:06
【问题描述】:

我正在开发一个使用 Thymeleaf 作为模板引擎的 Spring Boot 项目。我正在这个项目上设置 Swagger,所以我希望能够在我的 Thymeleaf 内容旁边提供静态内容。

“example.com/help”应该返回一个模板。

“example.com/docs”应该返回静态内容。

目前:

@RequestMapping("/docs")
    public String index() {
        return "index.html";
    }

返回这个:

org.thymeleaf.exceptions.TemplateInputException:错误解决 模板“index.html”,模板可能不存在或可能不存在 任何已配置的模板解析器均可访问

我不希望 Thymeleaf 解决这条路径。

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    简单的答案:如果您不希望 Thymeleaf/Spring MVC 处理您的请求,那么就不要要求它 :)

    更长的答案:当您在控制器中使用 @RequestMapping 时,您通常会填写一些模型并告诉 Spring MVC 使用视图来渲染该模型(这就是 Thymeleaf 的用武之地)。

    如果您想提供静态资源,则必须进行不同的配置。示例如下:

    @Component
    class WebConfigurer extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
             registry.addResourceHandler("/docs/**").addResourceLocations("file://path/to/yourDocs/");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-05
      • 2015-09-21
      • 2012-12-22
      • 2017-08-16
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2014-06-12
      相关资源
      最近更新 更多