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

标签 java spring spring-mvc spring-boot thymeleaf

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

使用的技术:

Spring Boot 1.4.2.RELEASE, Spring 4.3.4.发布, thymeleaf 2.1.5.发布, Tomcat 嵌入 8.5.6, 梅文3, Java 8

我想访问位于 ../src/main/resources/templates/mockups/index.html

的静态文件

所以我创建了这个 Controller :

@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/");
   }
}

关于java - Spring MVC - 通过 Thymeleaf 访问静态页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42598959/

相关文章:

java - 在 Java 8 中,如何对不带参数并返回 void 的方法进行方法引用?

java - ADB PULL 命令

Spring + Hibernate Search动态配置

java - 根据请求参数 Autowiring 请求范围的 bean

java - 我如何将此 Activity 转换为 fragment 并将 fragment 转换为导航

java - 最大独立组重量

java - 如何在 JBoss 上部署 JAR 文件?

spring - 在Grails中模拟帖子-Spring身份验证

java - Spring @WithMockUser 和 request.IsUserInRole()

java - 如何在运行时注册 WebSecurityConfigs?