SpringBoot返回html页面

在之前的一个项目里面,我们组员搭建的后台框架,可以在后台返回一个指定的HTML页面。

在他搭建的框架基础上照葫芦画瓢,在Controller里面写了返回页面的代码,也是可以返回我想要返回的HTML的页面的。

代码如下:

@Controller
public class UserController {
    @RequestMapping(value = "/hello",method = {RequestMethod.GET})
    public String hello(){
        return "hello";
    }
}

访问到了我放在templates文件夹下的hello.html。

然后今天,我自己搭建了一个新的SpringBoot项目,并且给项目注入了web依赖。

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
然后又 把那段代码原模原样Copy到我新建的项目的Controller文件里,然后也在templates文件夹下建了一个hello.html,结果 没有访问到页面,还给我报了一个错误。


我在网上各种搜这个错误的原因,尝试过网上的N种说法,都没有成功。

没办法,只能从头来,我比对了一下两个项目的关于这一部分的代码,结果发现,在原来的那个项目里面依赖了一个模板引擎:thymeleaf,代码如下:

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

我尝试着加上引擎依赖,然后重启服务器,结果能访问到了


结论:SpringBoot必须要依赖视图引擎才能返回视图。


猜你喜欢

转载自blog.csdn.net/i_dont_know_a/article/details/80720392