以前,我仅将 Spring MVC 与 jsp 文件一起使用。一切都很好。但今天我第一次尝试使用html文件,立刻就遇到了麻烦。由于某种原因,html 文件的内容永远不会显示在我的浏览器中。下面是配置。
配置:
@SpringBootApplication
@ComponentScan("newTestPackage")
public class Config extends WebMvcConfigurerAdapter {
public static void main(String[] args) throws Exception {
SpringApplication.run(Config.class, args);
}
@Bean
public ViewResolver jspViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
}
Controller :
@Controller
public class IndexController {
@RequestMapping(value = "/")
public String index() {
return "index";
}
}
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Hello World
</body>
</html>
JSP版本工作正常。 我不明白为什么 jsp 页面被找到并显示,而 html 页面却没有(状态“未找到”)
最佳答案
默认情况下,Spring Boot 提供 src/main/resources/static
文件夹中的 static
资源,因此请将 index.html
放入其中那个文件夹。
您可以引用here关于 Spring boot 如何为 Web 应用程序提供静态内容。
关于java - 将 Spring MVC 与 html 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43171465/