咱們作後臺開發時,不免會要開發管理後臺。以下面這樣的管理後臺,咱們徹底能夠不用h5同窗,本身快速開發管理web後臺的。javascript
因此本節就來教你們如何快速實現管理後臺表格數據的展現與操做。css
這節課是創建在你已經會建立springboot項目的基礎上,若是你還不知道怎麼建立一個springboot項目請異步到這裏:edu.csdn.net/course/deta…html
好了,下面就來說解,如何使用springboot+freemarker+bootstrap快速實現管理後臺表格數據的展現。java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
複製代碼
咱們freemarker使用的是 .ftl結尾的模版文件 web
<html>
<head>
<meta charset="utf-8">
<title>freemarker+bootstrap學習</title>
<#--本地引入-->
<#--<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">-->
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap .min.css">
</head>
<body>
<div class="container-fluid">
<div class="table-responsive">
<table id="dataGrid" class="table table-striped table-bordered">
<thead>
<tr>
<th width="50"><input type="checkbox" class="checkall"></th>
<th>標題</th>
<th>姓名</th>
<th>微信</th>
<th width="50">操做</th>
</tr>
</thead>
<tbody>
<#list demoList as row>
<tr>
<td>
<input type="checkbox" name="id" value="${row.id}">
</td>
<td>${row.title}</td>
<td>${row.name}</td>
<td>${row.wechat}</td>
<td>
<button class="btn btn-xs btn-primary"
onclick="deleteRow(${row.id})">刪除
</button>
</td>
</tr>
</#list>
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
function deleteRow(rowId) {
console.log("點擊了刪除", rowId);
}
</script>
</body>
</html>
複製代碼
這裏須要講下,咱們引入bootstrap有兩種方式,你們學習階段推薦用第一種方式。spring
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap .min.css">
複製代碼
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
複製代碼
這裏咱們定義一個DemoController以下數據庫
package com.qcl.demo.controller;
import com.qcl.demo.bean.Demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Created by qcl on 2019-04-29
* 微信:2501902696
* desc:freemarker學習
*/
@RestController
public class DemoController {
@GetMapping("/demoList")
public ModelAndView list(Map<String, Object> map) {
List<Demo> demoList = new ArrayList<>(4);
demoList.add(new Demo(1, "標題1", "編程小石頭1", "2501902696"));
demoList.add(new Demo(2, "標題2", "編程小石頭2", "2501902696"));
demoList.add(new Demo(3, "標題3", "編程小石頭3", "2501902696"));
demoList.add(new Demo(4, "標題4", "編程小石頭4", "2501902696"));
map.put("demoList", demoList);
return new ModelAndView("demo/list", map);
}
}
複製代碼
這裏咱們先模擬4條數據,而後把數據傳到咱們的list.ftl頁面,正常數據都應該是從數據庫裏取的,後面會作講解。 這就是用freemarker模版的好處。能夠直接在頁面裏使用咱們的java數據。編程
<table id="dataGrid" class="table table-striped table-bordered">
<thead>
<tr>
<th width="50"><input type="checkbox" class="checkall"></th>
<th>標題</th>
<th>姓名</th>
<th>微信</th>
<th width="50">操做</th>
</tr>
</thead>
<tbody>
<#list demoList as row>
<tr>
<td>
<input type="checkbox" name="id" value="${row.id}">
</td>
<td>${row.title}</td>
<td>${row.name}</td>
<td>${row.wechat}</td>
<td>
<button class="btn btn-xs btn-primary"
onclick="deleteRow(${row.id})">刪除
</button>
</td>
</tr>
</#list>
</tbody>
</table>
複製代碼
這樣咱們運行項目,而後進入web後臺,就能夠看到了,咱們這裏點擊刪除時,是能夠拿到每行的id的,這樣咱們就能夠根據id刪除具體數據,而後再從新刷新下頁面就能夠了。後面會給你們講解經過web後臺操做數據庫的。 bootstrap
到這裏咱們就實現了管理後臺表格數據的展現與操做了,是否是很簡單啊。springboot
我會把10小時實戰入門java系列課程錄製成視頻,若是你看文章不能很好的理解,能夠去看下視頻:edu.csdn.net/course/deta…
有任何關於編程的問題均可以加我微信2501902696(備註編程開發)