Java Spring,Whitelabel错误,让我的第一个项目进展顺利

时间:2015-11-21 22:59:32

标签: java spring

我无法使用html模板运行我的第一个Java Spring项目。我把模板放到src / main / resources / templates中。成功调用控制器方法。但是模板没有被调用,而是我得到了Whitelabel错误:

Whitelabel Error Page

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

Fri Nov 20 19:43:09 EST 2015
There was an unexpected error (type=Not Found, status=404).
No message available

有谁知道我在这里做错了什么?如果我将@ResponseBody添加到控制器,它会将字符串打印到屏幕上,但我无法弄清楚如何让模板通过。谢谢。

MasterSpringMvcApplication.java

package masterSpringMvc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MasterSpringMvcApplication {

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

HelloController.java

package masterSpringMvc.controllers;

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

@Controller
@RequestMapping("/")
public class HelloController {


    public String hello() {
        System.out.println("HelloController called!");
        return "resultPage";
    }
}

resultPage.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <meta charset="ISO-8859-1">
    <title>Hello thymeleaf</title>
</head>
<body>
    <span th:text="|Hello thymeleaf|">Chup Html</span>
</body>
</html>

的build.gradle

....
dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}
....

1 个答案:

答案 0 :(得分:0)

@RequestMapping("/")添加到控制器方法。在你的情况下 -

    @RequestMapping("/")
    public String hello() {
        System.out.println("HelloController called!");
        return "resultPage";
    }