Spring Boot v2.0没有看到模板

我尝试启动在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:


@Controller

public 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,则会显示该文件,但显然不会替换“名称”。


我做错了什么?


阿波罗的战车
浏览 154回答 3
3回答

海绵宝宝撒

Spring Boot将请求定向到匹配的控制器。因此,定义一个简单的Controller类来处理请求,并将该类放置在项目的根包下方或内部的包中:@Controllerpublic class MyController {&nbsp; &nbsp;@GetMapping(value={"/"})&nbsp; &nbsp;public ModelAndView getRoot()&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;return new ModelAndView("index");&nbsp; &nbsp;}}Controller-Annotation来自于包org.springframework.stereotype,而GetMapping-Annotation来自于org.springframework.web.bind.annotation。也许您还需要扩展pom.xml文件。检查它是否包含依赖项spring-boot-starter-web(如果已选择“ Maven Project”)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java