2016-12-13 124 views
1

這是我的第一個春天啓動例子,上邁出的第一步。我在STS 3.8中創建一個SpringBoot項目是這樣的:春天啓動的觀點不工作

文件 - >新建春季啓動項目
依賴條件 - >網絡

我的src/main/JAVA/com.first.boot

創建以下控制器

的IndexController 包com.first.boot;

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 

@Controller 
public class IndexController { 

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

接下來,我創建的視圖的index.html放在src /主/資源/模板:

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
<title>Getting Started: Serving Web Content</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
</head> 
<body> 
    Welcome to your first Spring Boot Page 
</body> 
</html> 

然後我跑項目爲: 右鍵單擊項目 - >運行方式 - >春啓動應用

控制檯顯示正確的映射和Tomcat啓動信息

Mapped "{[/]}" onto public java.lang.String com.first.boot.IndexController.index() 

Tomcat started on port(s): 8080 (http) 

Started FirstSpringBootApplication in 4.096 seconds (JVM running for 4.843) 

Initializing Spring FrameworkServlet 'dispatcherServlet' 

FrameworkServlet 'dispatcherServlet': initialization started 

FrameworkServlet 'dispatcherServlet': initialization completed in 64 ms 

但每當我本地主機上運行:在瀏覽器8080,我看到這個頁面:

Whitelabel Error Page 

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

Tue Dec 13 08:22:13 IST 2016 
There was an unexpected error (type=Not Found, status=404). 
No message available 

有控制檯沒有錯誤。

回答

1

添加以下依賴解決該問題:

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