繁体   English   中英

Spring MVC - 通过Thymeleaf访问静态页面

[英]Spring MVC - accessing a static page through Thymeleaf

我有一个Spring Boot Web应用程序,使用嵌入式Tomcat + Thymeleaf模板引擎,并将package作为可执行JAR文件。

使用的技术:

Spring Boot 1.4.2.RELEASE,Spring 4.3.4.RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat Embed 8.5.6,Maven 3,Java 8

我想访问位于../src/main/resources/templates/mockups/index.html的静态文件

所以我创建了这个控制器:

@Controller
public class MockupIndexController {

    @RequestMapping("/mockup/index")
    public String welcome(Map<String, Object> model) {
        return "/mockups/index.html";
    }

}

但是我收到了这个错误:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/mockups/index.html", template might not exist or might not be accessible by any of the configured Template Resolvers

在spring xml配置文件中,映射你的静态文件位置,请将静态文件保存在不同的文件夹中

 <mvc:resources mapping = "/mockups/**" location = "/src/main/resources/templates/mockups/" />

改变这一行

 return "/mockups/index.html";

 return "redirect:/mockups/index.html";

如果您没有使用配置文件,请添加此类

@Component
class WebConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/mockups/**").addResourceLocations("/src/main/resources/templates/mockups/");
   }
}

暂无
暂无

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

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