spring boot 返回html页面

本文详细介绍了如何在Spring Boot项目中整合Thymeleaf模板引擎,包括添加依赖、编写控制器、创建模板文件及配置属性。通过实例展示了如何在页面中显示列表和对象数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如下图所示:

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中去。不用再重新运行启动项目。当我使用此配置貌似也不是这样的,仍然需要重新运行项目。作为遗留问题,以后解决再更新。

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值