Spring-Boot错误:加载模板时出现404错误

时间:2017-09-30 02:18:15

标签: spring maven spring-mvc spring-boot

在我的春季启动应用程序中,我有以下pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

作为控制器我有如下的简单:

    @Controller
    public class ThyemeleafController {

        @RequestMapping("/")
        public String home() {
            return "home";
        }
    }

然后我在home.html中有一个简单的src/main/resources/templates,其中包含helloWorld。但是,当我运行应用程序并转到url:

http://localhost:8080/

它抱怨道:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Sep 29 17:15:13 GMT-12:00 2017
There was an unexpected error (type=Not Found, status=404).
No message available

1 个答案:

答案 0 :(得分:-1)

我得到了答案。我在另一个软件包中编写了我的控制器,所以我必须要求spring boot扫描该软件包并从中创建基本bean:

@SpringBootApplication
@ComponentScan("com.eventer.controller")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}