SpringBoot——web开发之Restful请求

博客介绍了Restful风格请求,通过URI和HTTP请求方式对资源进行CRUD操作。还讲述员工列表展示,利用Thymeleaf遍历和获取数据。重点介绍Thymeleaf抽取公共页面,包括抽取和引入方法,有th:insert、th:replace、th:include三种引入方式,还提及参数化抽取公共页面。

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

1、Restful风格请求

    请求的URI通过资源名称、资源标识以及HTTP请求的方式对资源进行CRUD操作,例如: /资源名称/{资源标识} 

2、员工列表

html:

<a class="nav-link" href="#" th:href="@{/emps}">
	<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" 	stroke-linejoin="round" class="feather feather-users">
		<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
		<circle cx="9" cy="7" r="4"></circle>
		<path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
		<path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
	</svg>
	员工管理
</a>

Controller:向页面传递数据可以使用Model、Map或者ModelMap

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;

    @GetMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
}

页面展示数据:

<table class="table table-striped table-sm">
	<thead>
		<tr>
			<th>ID</th>
			<th>姓名</th>
			<th>邮箱</th>
			<th>性别</th>
			<th>部门</th>
			<th>生日</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody>
		<tr th:each="emp:${emps}">
			<td th:text="${emp.id}"></td>
			<td>[[${emp.lastName}]]</td>
			<td th:text="${emp.email}"></td>
			<td th:text="${emp.gender} == 0 ? '女' : '男'"></td>
			<td th:text="${emp.department.departmentName}"></td>
			<td th:text="${#dates.format(emp.birth,'yyyy-MM-dd')}"></td>
			<td>
				<button class="btn btn-sm btn-primary">编辑</button>
				<button class="btn btn-sm btn-danger">删除</button>
			</td>
		</tr>
	</tbody>
</table>

利用th:each遍历数据,${}获取后台传递过来的数据,也可以使用thymeleaf内置的对象#dates格式化时间

效果:

3、Thymeleaf抽取公共页面

①抽取公共片段:使用th:fragment属性

<footer th:fragment="copy">
    &copy; 2011 The Good Thymes Virtual Grocery
</footer>

②引入公共片段:~{templatename::selector}:模板名::选择器或~{templatename::fragmentname}:模板名::片段名,如果使用th:insert等属性进行引入,可以不用写~{},但行内写法必须加上~{}:[[~{}]]、[(~{})]

有三种引入方式:

    th:insert:将公共片段整个插入到声明引入的元素中

<div th:insert="footer :: copy"></div>

    效果:

<div>
	<footer>
		&copy; 2011 The Good Thymes Virtual Grocery
	</footer>
</div>

    th:replace:将声明引入的元素替换为公共片段

<div th:replace="footer :: copy"></div>

效果:

<footer>
	&copy; 2011 The Good Thymes Virtual Grocery
</footer>

    th:include:将被引入的片段的内容包含进引入元素

<div th:include="footer :: copy"></div>

效果:

<div>
	&copy; 2011 The Good Thymes Virtual Grocery
</div>

4、参数化的抽取公共页面:向引入的公共片段传入参数,然后在公共片段中用${}取出传过来的参数进行判断实现一些逻辑等

引入公共片段时:

<div th:replace="commons/bar::#sidebar(activeUri='main')"></div>
<div th:replace="commons/bar::#sidebar(activeUri='list')"></div>

在公共片段中获取引入时传的参数:

<a class="nav-link active" th:class="${activeUri == 'main' ? 'nav-link active':'nav-link'}" href="#" th:href="@{/main.html}">
	<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">
		<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
		<polyline points="9 22 9 12 15 12 15 22"></polyline>
	</svg>
	Dashboard <span class="sr-only">(current)</span>
</a>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值