[英]How to serve static html content page in spring-boot
我正在通过spring-boot
嵌入式 tomcat,并希望将静态index.html
页面作为正在运行的应用程序的一部分提供服务。
但以下不起作用:
@SpringBootApplication
public class HMyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RestController
public class HomeContoller {
@RequestMapping("/")
public String index() {
return "index";
}
}
src/main/resources/static/index.html
结果:当我调用localhost:8080
,我只看到“index”这个词,而不是我的 html 页面。 为什么?
我的错:我有一个带有@EnableWebMvc
注释的附加类。 这以某种方式弄乱了 spring-boot 自动配置。 我删除了它,现在它可以返回index.html
。
对我来说这行得通,我相信有更好的方法(比如没有 .html )。
@RequestMapping("/")
public String index() {
return "index.html";
}
您可以使用ModelAndView
在 Spring Boot 中提供静态 HTML 内容。
@RequestMapping("/")
public ModelAndView home()
{
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
application.properties:-
spring.mvc.view.suffix = .html
HTML 文件:- src/main/resources/static/index.html
那是因为@RestController 注释,删除这个注释对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.