HTML、AJAX、SpringBoot、Vite增删改查
时间: 2023-09-23 08:04:39 浏览: 99
SpringBoot增删改查
HTML 是一种用于创建网页的标记语言,它可以定义网页的结构和内容。AJAX 是一种异步的Web开发技术,可以在不刷新整个页面的情况下向服务器发送请求和接收响应。SpringBoot 是一个快速开发Web应用程序的框架,可以帮助开发者快速搭建Web应用。Vite 是一个现代化的前端构建工具,可以帮助开发者快速构建现代化的Web应用。
增删改查(CRUD)是Web应用程序中常用的操作,它指的是对数据进行增加(Create)、读取(Retrieve)、更新(Update)和删除(Delete)的操作。
下面是一个使用 HTML、AJAX、SpringBoot 和 Vite 进行增删改查的例子:
1. 创建一个 HTML 页面,用于显示数据和提供操作界面。
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CRUD Example</title>
</head>
<body>
<h1>CRUD Example</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="data-table">
</tbody>
</table>
<form id="data-form">
<input type="hidden" id="id" name="id">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<button type="submit" id="submit-btn">Submit</button>
<button type="button" id="cancel-btn">Cancel</button>
</form>
</body>
</html>
```
2. 使用 AJAX 向服务器发送请求和接收响应。
```
function loadData() {
$.ajax({
url: "/api/data",
type: "GET",
dataType: "json",
success: function(data) {
var tableBody = "";
data.forEach(function(record) {
tableBody += "<tr>";
tableBody += "<td>" + record.id + "</td>";
tableBody += "<td>" + record.name + "</td>";
tableBody += "<td>" + record.email + "</td>";
tableBody += "<td>";
tableBody += "<button type='button' class='edit-btn' data-id='" + record.id + "'>Edit</button>";
tableBody += "<button type='button' class='delete-btn' data-id='" + record.id + "'>Delete</button>";
tableBody += "</td>";
tableBody += "</tr>";
});
$("#data-table").html(tableBody);
}
});
}
function saveData() {
var formData = $("#data-form").serialize();
var url = $("#id").val() ? "/api/data/" + $("#id").val() : "/api/data";
var method = $("#id").val() ? "PUT" : "POST";
$.ajax({
url: url,
type: method,
data: formData,
success: function() {
$("#data-form").trigger("reset");
$("#id").val("");
loadData();
}
});
}
function deleteData(id) {
$.ajax({
url: "/api/data/" + id,
type: "DELETE",
success: function() {
loadData();
}
});
}
$(document).ready(function() {
loadData();
$("#cancel-btn").click(function() {
$("#data-form").trigger("reset");
$("#id").val("");
});
$("#submit-btn").click(function(event) {
event.preventDefault();
saveData();
});
$(document).on("click", ".edit-btn", function() {
var id = $(this).data("id");
$.ajax({
url: "/api/data/" + id,
type: "GET",
dataType: "json",
success: function(record) {
$("#id").val(record.id);
$("#name").val(record.name);
$("#email").val(record.email);
}
});
});
$(document).on("click", ".delete-btn", function() {
var id = $(this).data("id");
deleteData(id);
});
});
```
3. 在服务器端使用 SpringBoot 提供 RESTful API。
```
@RestController
@RequestMapping("/api/data")
public class DataController {
@Autowired
private DataRepository dataRepository;
@GetMapping
public List<Data> getAllData() {
return dataRepository.findAll();
}
@GetMapping("/{id}")
public Data getData(@PathVariable Long id) {
return dataRepository.findById(id).orElse(null);
}
@PostMapping
public void createData(@RequestBody Data data) {
dataRepository.save(data);
}
@PutMapping("/{id}")
public void updateData(@PathVariable Long id, @RequestBody Data data) {
data.setId(id);
dataRepository.save(data);
}
@DeleteMapping("/{id}")
public void deleteData(@PathVariable Long id) {
dataRepository.deleteById(id);
}
}
```
4. 使用 Vite 构建前端应用程序。
```
import { createApp } from "vue";
import App from "./App.vue";
createApp(App).mount("#app");
```
5. 在 App.vue 中使用 HTML 和 JavaScript。
```
<template>
<div>
<h1>CRUD Example</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="record in records" :key="record.id">
<td>{{ record.id }}</td>
<td>{{ record.name }}</td>
<td>{{ record.email }}</td>
<td>
<button @click="editRecord(record)">Edit</button>
<button @click="deleteRecord(record.id)">Delete</button>
</td>
</tr>
</tbody>
</table>
<form @submit.prevent="saveRecord">
<input type="hidden" v-model="record.id">
<label for="name">Name:</label>
<input type="text" v-model="record.name" required>
<br>
<label for="email">Email:</label>
<input type="email" v-model="record.email" required>
<br>
<button type="submit">Submit</button>
<button type="button" @click="cancelRecord">Cancel</button>
</form>
</div>
</template>
<script>
import axios from "axios";
export default {
name: "App",
data() {
return {
records: [],
record: {
id: null,
name: "",
email: ""
}
};
},
mounted() {
this.loadData();
},
methods: {
loadData() {
axios.get("/api/data").then(response => {
this.records = response.data;
});
},
saveRecord() {
axios({
method: this.record.id ? "put" : "post",
url: this.record.id ? `/api/data/${this.record.id}` : "/api/data",
data: this.record
}).then(() => {
this.cancelRecord();
this.loadData();
});
},
deleteRecord(id) {
axios.delete(`/api/data/${id}`).then(() => {
this.loadData();
});
},
editRecord(record) {
this.record = {
id: record.id,
name: record.name,
email: record.email
};
},
cancelRecord() {
this.record = {
id: null,
name: "",
email: ""
};
}
}
};
```
阅读全文