Spring MVC - 通过 Thymeleaf 访问静态页面
Spring MVC - accessing a static page through Thymeleaf
我有一个 Spring 启动 Web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
使用的技术:
Spring 引导 1.4.2.RELEASE,
Spring 4.3.4.RELEASE,
百里香 2.1.5.RELEASE,
Tomcat 嵌入 8.5.6,
行家 3,
Java8
我想访问位于 ../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
在springxml配置文件中,映射您的静态文件位置,请将静态文件放在不同的文件夹中
<mvc:resources mapping = "/mockups/**" location = "/src/main/resources/templates/mockups/" />
更改此行
return "/mockups/index.html";
到
return "redirect:/mockups/index.html";
如果您不使用配置文件,请添加此 class
@Component
class WebConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/mockups/**").addResourceLocations("/src/main/resources/templates/mockups/");
}
}
我有一个 Spring 启动 Web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
使用的技术:
Spring 引导 1.4.2.RELEASE, Spring 4.3.4.RELEASE, 百里香 2.1.5.RELEASE, Tomcat 嵌入 8.5.6, 行家 3, Java8
我想访问位于 ../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
在springxml配置文件中,映射您的静态文件位置,请将静态文件放在不同的文件夹中
<mvc:resources mapping = "/mockups/**" location = "/src/main/resources/templates/mockups/" />
更改此行
return "/mockups/index.html";
到
return "redirect:/mockups/index.html";
如果您不使用配置文件,请添加此 class
@Component
class WebConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/mockups/**").addResourceLocations("/src/main/resources/templates/mockups/");
}
}