我无法让我的第一个Java Spring项目与html模板一起运行。我将模板放入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')
}
....
最佳答案
将@RequestMapping("/")
添加到控制器方法。就您而言-
@RequestMapping("/")
public String hello() {
System.out.println("HelloController called!");
return "resultPage";
}