我正在使用 Spring Boot 并有一个 html 文件“index.html”,并且我有一个类“Functions”。基本上,我想要发生的是,当运行函数类时,当我转到 localhost:8080 时,html 模板会出现在 localhost 上。 我该怎么做?
这是 Functions 类的一部分:
public class Functions{
@RequestMapping("/")
public void getHomepage(){
//Return the html-template
}
}
html 文件名为“index.html”,位于 src/main/resources/static-folder 中。
谢谢!
最佳答案
首先,将index.html
模板文件放入src/main/resources/templates
而不是src/main/resources/static
.
您的类Functions
必须是Spring MVC Controller ;您可以通过添加 @Controller 注释使其成为 Controller 。在 getHomepage()
方法中返回模板的名称:
@Controller
public class Functions {
@RequestMapping("/")
public String getHomepage() {
return "index";
}
}
这就是基本的 Spring Web MVC;看这样的教程Serving Web Content Spring 网站上的指南和 reference documentation .
关于java - 如何使用 Spring Boot 使 html 文件出现在本地主机上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38221461/