Spring 简单地渲染一个 html 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25330084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring simply rendering an html page
提问by Gustavo Semi?o-Lobo
The problem:
问题:
Using Spring 4, I am getting this when visiting a webpage
使用 Spring 4,我在访问网页时得到这个
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Aug 15 16:41:29 BST 2014
There was an unexpected error (type=Not Found, status=404).
What I have:
我拥有的:
I have this Main class:
我有这个主课:
// src/main/java/abc/Main.java
package abc;
import abc.web.WebAppConfig;
import org.springframework.boot.SpringApplication;
public class Main {
public static void main(String[] args) {
SpringApplication.run(WebAppConfig.class);
}
}
Then I have this WebAppConfig.class (currently with just some configuration annotations):
然后我有这个 WebAppConfig.class (目前只有一些配置注释):
// src/main/java/abc/web/WebAppConfig.java
package abc.web;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class WebAppConfig {
}
And this controller HomeController.java:
这个控制器 HomeController.java:
// src/main/java/abc/web/HomeController.java
package abc.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping(method = GET)
public String home() {
System.out.println("HELLO !!");
return "home";
}
}
The HELLO !! shows up in the logs.
你好!!出现在日志中。
And finally I have a html file at src/main/java/abc/webapp/home.html, with just some html tags including a ptag with Hello, world!.
最后我有一个 html 文件src/main/java/abc/webapp/home.html,只有一些 html 标签,包括一个p带有Hello, world!.
The question:
问题:
I understand that I am missing the way of rendering the view, but I searched a couple of questions on stackoverflow and haven't find a solution yet.
我知道我缺少渲染视图的方式,但是我在 stackoverflow 上搜索了几个问题,但还没有找到解决方案。
Can someone explain how can I get Spring to render a webpage ? What am I missing ?
有人可以解释如何让 Spring 呈现网页吗?我错过了什么?
Thanks in advance :)
提前致谢 :)
回答by geoand
Spring Boot will automatically use and configure Thymeleaf as the view rendering engine, as long as it's on the classpath. To put it on the classpath use
Spring Boot 会自动使用和配置 Thymeleaf 作为视图渲染引擎,只要它在类路径上。把它放在类路径上使用
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
in the gradle build file.
在 gradle 构建文件中。
If you are using maven add the dependency:
如果您使用 maven 添加依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
In your case in order to display the home.htmlview (in accordance to the controller you are using), you need to place it under /resources/templates.
在您的情况下,为了显示home.html视图(根据您使用的控制器),您需要将其放在/resources/templates.
For a complete example, check out thisguide.
有关完整示例,请查看本指南。
回答by kryger
The simplest answer to the "how can I get Spring Boot to render a webpage?" question is: place your home.html file inside src/main/resources/static/folder. The page will be available under the /home.htmlURL.
“如何让 Spring Boot 呈现网页?”的最简单答案 问题是:将您的 home.html 文件放在src/main/resources/static/文件夹中。该页面将在/home.htmlURL下可用。
More details in the documentation.
文档中的更多详细信息。
回答by kulatamicuda
You probably not configured template engine and view resolver. See example with thymeleaf here http://www.thymeleaf.org/doc/thymeleafspring.htmlAs thymeleaf template is a valid HTML code and vice verse you can use it.
您可能没有配置模板引擎和视图解析器。在此处查看 thymeleaf 示例http://www.thymeleaf.org/doc/thymeleafspring.html由于 thymeleaf 模板是有效的 HTML 代码,反之亦然,您可以使用它。

