如下图所示:

spring boot 返回html页面_html

1、在pom文件中增加thymeleaf依赖

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

2、PageController代码(此处所用到的注解是@Controller而不是@RestController)

package com.example.api.ReturnPage.controller;

import com.example.api.ReturnPage.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XieZhiXin on 2018/7/30.
 */
@Controller
@RequestMapping("/html")
public class PageController {

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/test")
    public String test() {
        //int i = 1 / 0;//服务器内部运行异常 跳转500页面
        return "index";
    }

    @GetMapping("/user")
    public String testUser(Model model) {
        List<User> userList=new ArrayList<>() ;
        User user=new User("admin", "123456", "管理员");
        User user1=new User("admin1", "123456", "管理员1");
        userList.add(user);
        userList.add(user1);
        model.addAttribute("users",userList);
        model.addAttribute("user",user);
        model.addAttribute("user1",user1);
        return "userInfo";
    }
}

3、在templates文件夹下新建返回的页面,以及error情况下返回的页面

index页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页面</title>
</head>
<body>
 Hello Html!
</body>
</html>

 userInfo页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
</head>
<body>
********************************List*****************************************
<form action="" th:each="user : ${users}" >
    用户编号:<input name="id" th:value="${user.account}"/><br>
    用户姓名:<input type="text" name="password" th:value="${user.name}"/><br>
    登录密码:<input type="text" name="username" th:value="${user.getPassword()}"/>
</form>
**********************************对象****************************************
<form action="" th:object="${user1}">
    用户编号:<input name="id" th:value="${user1.getAccount()}"/><br>
    用户姓名:<input type="text" name="password" th:value="${user1.name}"/><br>
    登录密码:<input type="text" name="username" th:value="${user1.getPassword()}"/>
</form>
</body>
</html>

error包下的404、500页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
这是404页面
</body>
</html>

4、application.properties配置文件如下


# 定位模板的目录,spring boot此配置默认为classpath:/templates/
spring.mvc.view.prefix=classpath:/templates/
# 给返回的页面添加后缀名,spring boot默认为.html
spring.mvc.view.suffix=.html

# thymeleaf缓存设置
spring.thymeleaf.cache=false
server.tomcat.access_log_enabled=true
server.tomcat.basedir:target/tomcat

application.properties配置参考

既然Springboot默认的模板路径为templates下,也可配置,我也尝试修改了此路径,但我的项目并没有起到任何效果,没搞懂为什么。还有一个问题,thymeleaf缓存的问题,Springboot使用thymeleaf默认是有缓存的,也就是将页面代码改了之后并不会及时刷新页面代码,你必须重新运行Springboot的Application方法才能看到页面修改后的效果。如果将thymeleaf的缓存关掉,也就是当页面代码修改后自动发布到Springboot内嵌的tomcat中去。不用再重新运行启动项目。当我使用此配置貌似也不是这样的,仍然需要重新运行项目。作为遗留问题,以后解决再更新。

如果有哪位大牛路过,还望您不吝赐教,谢谢!

新开通一个个人微信公众号,感兴趣的朋友可以扫描点击关注下哦,在接下的工作中的所感所想、优质资源也会在公众号内更新,希望我们可以一起交流共同进步

spring boot 返回html页面_html_02