<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Room Management</title>
</head>
<body>
<h2>Room Management</h2>
<!-- Form to Add New Room -->
<form th:action="@{/admin/rooms/add}" method="post" th:object="${room}">
<div>
<label for="name">Room Name:</label>
<input type="text" id="name" th:field="*{name}" required>
</div>
<div>
<label for="location">Location:</label>
<input type="text" id="location" th:field="*{location}" required>
</div>
<div>
<label for="capacity">Capacity:</label>
<input type="number" id="capacity" th:field="*{capacity}" required>
</div>
<div>
<button type="submit">Add Room</button>
</div>
</form>
<h3>Existing Rooms</h3>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Location</th>
<th>Capacity</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="room : ${rooms}">
<td th:text="${room.id}"></td>
<td th:text="${room.name}"></td>
<td th:text="${room.location}"></td>
<td th:text="${room.capacity}"></td>
<td>
<!-- Form to Update Room -->
<form th:action="@{/admin/rooms/update}" method="post" th:object="${room}">
<input type="hidden" th:field="*{id}">
<input type="text" th:field="*{name}" required>
<input type="text" th:field="*{location}" required>
<input type="number" th:field="*{capacity}" required>
<button type="submit">Update</button>
</form>
<!-- Form to Delete Room -->
<form th:action="@{/admin/rooms/delete}" method="post">
<input type="hidden" name="id" th:value="${room.id}">
<button type="submit">Delete</button>
</form>
</td>
</tr>
</tbody>
</table>
</body>
</html>
package com.example.meetingroombooking.controller;
import com.example.meetingroombooking.model.Room;
import com.example.meetingroombooking.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/admin/rooms")
public class RoomManagementController {
@Autowired
private RoomService roomService;
@GetMapping
public String listRooms(Model model) {
List<Room> rooms = roomService.getAllRooms();
model.addAttribute("rooms", rooms);
model.addAttribute("room", new Room()); // For the Add Room form
return "roomManagement";
}
@PostMapping("/add")
public String addRoom(@ModelAttribute Room room) {
roomService.saveRoom(room);
return "redirect:/admin/rooms";
}
@PostMapping("/update")
public String updateRoom(@ModelAttribute Room room) {
roomService.saveRoom(room);
return "redirect:/admin/rooms";
}
@PostMapping("/delete")
public String deleteRoom(@RequestParam Long id) {
roomService.deleteRoom(id);
return "redirect:/admin/rooms";
}
}
标签:会议,Room,管理,roomService,rooms,import,public,room From: https://www.cnblogs.com/lz2z/p/18319786