SpringBoot整合thymeleaf实现显示所有用户(二)

4 篇文章 0 订阅
2 篇文章 0 订阅

这次想要前端通过thymeleaf动态实现显示用户信息的操作
结果演示:
数据库user表数据
在这里插入图片描述
页面展示所有数据
在这里插入图片描述
代码布局格式
这里需要一个service,dao,bean,controller,以及实现service的接口、实现dao的接口
在这里插入图片描述
UserController .java

@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
    private UsersService userService;
     /**
     * 查询全部用户
     */
    @GetMapping("/findUserAll")
    public String findUserAll(Model model){
        List<Users> list = null;
        try {
            list = this.userService.findUsersAll();
            model.addAttribute("list",list);
        }
        catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "showuser";
    }
}

PageController .java

@Controller
public class PageController {
    /**
     * 定义一个页面跳转的方法
     */
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        System.out.println(page);
        return page;
    }
}

Users .java

public class Users {
    private String userid;
    private String username;
    private String usersex;

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsersex() {
        return usersex;
    }

    public void setUsersex(String usersex) {
        this.usersex = usersex;
    }
}

UsersService .java

public interface UsersService {
    List<Users> findUsersAll();
}

UserServiceImpl .java

@Service
public class UserServiceImpl implements UsersService {
 @Override
    public List<Users> findUsersAll() {
        return this.usersDao.selectUsersAll();
    }
 }

UsersDao .java

public interface UsersDao {
    List<Users> selectUsersAll();
}
@Repository
public class UsersDaoImpl implements UsersDao {
	@Override
	    public List<Users> selectUsersAll() {
	        String sql = "select * from users";
	        return this.jdbcTemplate.query(sql, new RowMapper<Users>() {
	            /**
	             * 结果集的映射
	             * @param resultSet
	             * @param i
	             * @return
	             * @throws SQLException
	             */
	            @Override
	            public Users mapRow(ResultSet resultSet, int i) throws SQLException {
	                Users users = new Users();
	                users.setUserid(resultSet.getString("userid"));
	                users.setUsername(resultSet.getString("username"));
	                users.setUsersex(resultSet.getString("usersex"));
	                return users;
	            }
	        });
	    }
	  }

后端就是这部分内容了,逻辑结构应该已经很清楚了。现在就是如何在前端显示这些数据
在这里插入图片描述
在resources里的templates中创建一个html,显示所有数据
showuser.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<link rel="shortcut icon" href="../resource/favicon.ico" th:href="@{/static/favicon.ico}">
<head>
    <title>Title</title>
    <style type="text/css">
        body{
            background-color: antiquewhite;
        }
        table{
            margin-top: 5%;
            width: 80%;
            text-align: center;
        }
        table tr th{
            background-color: aliceblue;
        }
        .find{
            height: 30px;
            width: 200px;
            position: absolute;
            left: 20%;
            }
        button{
            height: 30px;
            position: absolute;
            left: 34%;
            top:14.5%;
        }
    </style>
</head>
<body>
    <h1 style="text-align: center">所有用户</h1>
    <a th:href="@{/addUser}">添加用户</a><br>
    <form th:action="@{/user/findUserById}" method="post">
        <input placeholder="请输入你要查找的用户" class="find" name="userid"/>
        <button type="submit">查找</button>
    </form>
    <table border="1"align="center">
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>性别</th>
            <th>操作</th>
        </tr>
        <tr th:each="u:${list}">
            <td th:text="${u.userid}"></td>
            <td th:text="${u.username}"></td>
            <td th:text="${u.usersex}"></td>
            <td>
                <a th:href="@{/user/preUpdateUser(id=${u.userid})}">修改</a>
                <a th:href="@{/user/deleteUser(id=${u.userid})}">删除</a>
            </td>
        </tr>
    </table>
</body>
</html>

然后访问http://localhost:8080/user/findUserAll就可以访问数据库中的数据了。
(前提是数据库有此表。我这里没有把pom.xml的配置写出来。以及要引入数据库依赖,thymeleaf依赖)
在这里插入图片描述
有啥不懂的或者代码部分可以交流的可以私聊我

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot 可以很方便的集成 Thymeleaf 模板引擎,下面是整合步骤: 1.添加 Thymeleaf 依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2.配置 Thymeleaf 模板 在 `src/main/resources/templates/` 目录下创建一个 thymeleaf 模板文件,例如 index.html: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf Demo</title> </head> <body> <h1 th:text="${message}">Hello World!</h1> </body> </html> ``` 其中 `${message}` 表示从后台传递过来的数据。 3.配置视图解析器 在 application.properties 或 application.yml 文件中添加以下配置: ```yaml spring: thymeleaf: cache: false prefix: classpath:/templates/ suffix: .html encoding: utf-8 ``` 其中: - `cache` 表示是否开启缓存 - `prefix` 表示模板文件所在目录 - `suffix` 表示模板文件后缀 - `encoding` 表示模板文件编码 4.在 Controller 中使用 Thymeleaf 在 Controller 中设置需要传递到前端的数据,并指定要返回的模板文件名: ```java @Controller public class DemoController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, Thymeleaf!"); return "index"; } } ``` 其中: - `@Controller` 表示这是一个控制器 - `@GetMapping("/hello")` 表示处理 GET 请求,路径为 /hello - `Model` 用于存储需要传递到前端的数据 - `return "index"` 表示返回名为 index 的模板文件 5.运行项目 启动 Spring Boot 项目,访问 http://localhost:8080/hello 即可看到效果。 以上就是 Spring Boot 整合 Thymeleaf 的基本步骤,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值