【发布时间】:2019-01-23 00:31:13
【问题描述】:
我使用 Maven 创建了一个简单的 Spring Boot Web 应用程序。尝试在点击 / 页面时返回一个 HTML 页面,但它没有返回抛出白标错误页面。
我的主要课程:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProductLaunchReportingApplication {
public static void main(String[] args) {
SpringApplication.run(ProductLaunchReportingApplication.class, args);
}
}
控制器:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
//@ResponseBody
public class HomeController {
@RequestMapping("/")
public String viewHome() {
return "home";
}
}
应用程序属性:
server.port=9991
home.html :(位置:src/main/resources/static)
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<p>Hello From Home Page</p>
</body>
</html>
【问题讨论】:
-
你能不能显示你的 pom.xml 以及 ProductLaunchReportingApplication 和 HomeController 在哪个包中
-
stackoverflow.com/a/51555158/6446770 看到这个以获得详细的答案。也阅读 cmets 以获得更好的理解。
-
src/main/resources/static下的东西不是使用 ViewResolver 解决的,而是直接解决的。所以/home.html会起作用。如果这不是您想要的,请包含 Thymeleaf 之类的内容并将文件放在src/main/resources/templates而不是static。
标签: java spring maven spring-boot