我尝试启动在Spring Initializr中创建的演示应用程序。Spring Boot版本-2.0.3。我创建了index.html并将其放入resources/templates文件夹中。当我启动时spring-boot:run,控制台状态中的消息之一:Tomcat started on port(s): 8080 (http) with context path ''当我打开时localhost:8080,它给了我Spring的Whitelabel错误页面我试图server.servlet.context-path=/在resources/application.properties文件中添加行。但这无济于事。如何为索引设置正确的路径?UPD:似乎问题仅在于模板部分。内容index.html在resources/templates:<!DOCTYPE html><html><body><h1>Hello {{ name }}!!!</h1></body></html>内容IndexController.java:@Controllerpublic class IndexController { @GetMapping("/") public ModelAndView index() { Map<String, String> model = new HashMap<>(); model.put("name", "world"); return new ModelAndView("index", model); }我在使用胡子pom.xml:<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mustache</artifactId> </dependency> <dependency>它给了我whitelabel错误。即使server.servlet.context-path=/在resources/application.properties如果将文件放在中resource/static,然后在中将文件重命名为“ index.html” IndexController,则会显示该文件,但显然不会替换“名称”。我做错了什么?
3 回答
![?](http://img1.sycdn.imooc.com/533e4d00000171e602000200-100-100.jpg)
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
Spring Boot将请求定向到匹配的控制器。因此,定义一个简单的Controller类来处理请求,并将该类放置在项目的根包下方或内部的包中:
@Controller
public class MyController {
@GetMapping(value={"/"})
public ModelAndView getRoot()
{
return new ModelAndView("index");
}
}
Controller-Annotation来自于包org.springframework.stereotype,而GetMapping-Annotation来自于org.springframework.web.bind.annotation。
也许您还需要扩展pom.xml文件。检查它是否包含依赖项spring-boot-starter-web(如果已选择“ Maven Project”)。
添加回答
举报
0/150
提交
取消