春天只是呈现一个html页面 [英] Spring simply rendering an html page

查看:276
本文介绍了春天只是呈现一个html页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

使用Spring 4,我在访问网页时会收到此消息

Using Spring 4, I am getting this when visiting a webpage

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).

我所拥有的:

我有这个主班:

// 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);

    }
}

然后我有这个WebAppConfig.class(当前仅带有一些配置注释):

Then I have this WebAppConfig.class (currently with just some configuration annotations):

// 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 {

}

这个控制器HomeController.java:

And this controller 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";
    }
}

你好!显示在日志中.

最后,我在src/main/java/abc/webapp/home.html有一个html文件,其中只有一些html标签,其中包括带有Hello, world!p标签.

And finally I have a html file at src/main/java/abc/webapp/home.html, with just some html tags including a p tag with Hello, world!.

问题:

我了解我缺少呈现视图的方式,但是我搜索了有关stackoverflow的几个问题,但尚未找到解决方案.

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.

有人可以解释一下我如何让Spring呈现网页吗?我想念什么?

Can someone explain how can I get Spring to render a webpage ? What am I missing ?

先谢谢您了:)

推荐答案

Spring Boot将自动使用Thymeleaf并将其配置为视图渲染引擎,只要它位于类路径中即可. 要将其放在类路径中,请使用

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

compile("org.springframework.boot:spring-boot-starter-thymeleaf")

在gradle构建文件中.

in the gradle build file.

如果您使用的是maven,请添加依赖项:

If you are using maven add the dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

要显示home.html视图(根据您使用的控制器),您需要将其放置在/resources/templates下.

In your case in order to display the home.html view (in accordance to the controller you are using), you need to place it under /resources/templates.

有关完整示例,请查看指南.

For a complete example, check out this guide.

这篇关于春天只是呈现一个html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆