2016-05-25 61 views
1

我坚持使用这个简单的MVC示例。当我启动应用程序并转到localhost:8080时,我得到了“Whitelabel Error Page”,即使我在“src/main/resources/templates”中创建了“index.html”。我还在我的索引方法中添加了@RequestMapping(“/”)。我找不到问题。Springboot白标错误页面

IndexController.java

package controllers; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
@Controller 
public class IndexController { 
    @RequestMapping("/") 
    public String index(){ 
    return "index"; 
    } 
} 

SpringmvcApplication.java

package com.example; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
@SpringBootApplication 
public class SpringmvcApplication { 
    public static void main(String[] args) { 
    SpringApplication.run(SpringmvcApplication.class, args); 
    } 
} 

index.html - “的src /主/资源/模板” 下:

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head lang="en"> 
<title>Hello Spring MVC</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
</head> 
<body> 
<h1>Hello World</h1> 
<h2>This is my Thymeleaf index page.</h2> 
</body> 
</html> 
+0

它在错误页面上显示什么信息?你有没有看到日志上的任何错误? –

+0

白标签错误页面 此应用程序没有明确的映射/错误,因此您将此视为后备。 Wed May 25 22:55:44 CEST 2016 出现意外错误(type = Not Found,status = 404)。 无可用消息 - 我可以在localhost上看到:8080 – zoram

+0

启用调试(请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html)以查看更多消息。 –

回答

1

正如你可以看到记录您控制器未被Spring发现并注册。可能是因为它属于未被自动扫描的类。为了解决这个问题,我建议将代码的结构更新到structure that is advised in the documentation。解决这个问题的另一种方法是尝试手动指定@ComponentScan

+0

谢谢! Slava,是的,我也想到我的包装结构。我使用了Spring工具Suite 3.7.2.RELEASE,并在“com.example”下创建了我的“控制器”包,但IDE没有将“com.example”作为子包装。也许STS 3.7.2有一些错误呢?我没有更新到最新的STS版本。 – zoram

+0

我不知道它是否是STS中的错误。另外我不使用STS。你是否已经试图将'IndexController'从'controllers'移动到'com.example.controllers'? –

+0

感谢大家!是的,它现在工作。控制器包现在位于包com.example.comtrollers;我完全与Spring STS IDE和IntelliJ IDEA IDE及其项目结构混淆。这两个IDE有完全不同的封装结构吗? – zoram