使用thymeleaf与jquery实现页面的局部刷新(以显示学校的系为例)

假设有这么一个需求,我们需要在前端展示当前学校有多少个系。这个系的数据不能写死,因为后面可能随时会修改或者增加系的信息,而写死后每次修改系信息后就要回来修改前端代码,这是很麻烦的。这一篇博客就来记录一下怎么操作。


有个思路就是后端写好接口,用来查询数据库获取当前的所有系后,存储到request域中。然后前端每次访问该页面都请求一次该接口,得到数据后遍历并填充到下拉表单中(select).

这里servicedao的代码略过了,就是查询数据库并返回,po出controller的代码:

controller:

    //遍历所有部门,将结果保存到request域中
    @GetMapping("/get")
    public String getAllDepartment(HttpServletRequest request){
        List<Department> allDepartment = departmentService.getAllDepartment();

        request.setAttribute("allDepartment",allDepartment);

		//demo是html页面的名字,department是表示你要刷新到哪个区域,th:fragment='department'
        return "demo::department";
    }

html代码:

<form>
	<!--th:fragment="department"标记一下该区域为要刷新请求的区域,这个区域的id为department-->
    <div id="department" th:fragment="department">
    <select>
    <!--遍历request域中的数据,并填充到下拉表单中-->
        <option th:each="department:${allDepartment}" th:value="${department['department_id']}" th:text="${department['department_name']}"></option>
    </select>
    </div>
</form>

jquery的代码:

    <script src="webjars/jquery/3.5.1/jquery.min.js"></script>

    <script>
		//页面加载时调用函数
        $(function () {
        	//发送请求到对应请求路径
            $('#department').load("/department/get");
        })
    </script>

运行结果:

在这里插入图片描述

并且每一个选项都有对应的id值:

在这里插入图片描述

bingo~

Thymeleaf是一个模板引擎,它可以与Bootstrap和JQuery集成使用。下面是一些集成的步骤: 1. 引入Bootstrap和JQuery的CSS和JS文件 在HTML文件中,引入Bootstrap和JQuery的CSS和JS文件: ```html <!-- 引入 Bootstrap 的 CSS --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- 引入 jQuery --> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> <!-- 引入 Bootstrap 的 JS 文件 --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> ``` 2. 在Thymeleaf使用Bootstrap和JQuery的标记 在Thymeleaf的模板文件中,可以使用Bootstrap和JQuery的标记来渲染页面: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf Bootstrap jQuery Demo</title> <!-- 引入 Bootstrap 的 CSS --> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <!-- 引入 jQuery --> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> <!-- 引入 Bootstrap 的 JS 文件 --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h1 th:text="${title}"></h1> <table class="table"> <thead> <tr> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> <tr th:each="user : ${users}"> <td th:text="${user.name}"></td> <td th:text="${user.email}"></td> </tr> </tbody> </table> <button type="button" class="btn btn-primary">Save</button> </div> </body> </html> ``` 在上面的例子中,使用Bootstrap的表格和按钮,以及JQuery的循环。 3. 在Controller中传递数据 在Controller中,可以使用Thymeleaf的Model对象来传递数据: ```java @Controller public class UserController { @GetMapping("/") public String index(Model model) { List<User> users = new ArrayList<>(); users.add(new User("Alice", "alice@example.com")); users.add(new User("Bob", "bob@example.com")); model.addAttribute("title", "User List"); model.addAttribute("users", users); return "index"; } } ``` 在上面的例子中,将用户列表传递给模板,以便在页面中渲染。 这样,ThymeleafBootstrap和JQuery就可以集成使用了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值