我正在處理一個頁面,我必須從資料庫中檢索特定日期的“任務”。我目前的方法是在服務器上使用 GetMapping,并回傳任務串列
下面是我的一部分TaskController
@Controller
@RequestMapping()
public class TaskController {
@Autowired
private TaskService taskService;
@GetMapping("/calendar/{date}")
public String displayTasksByClick(@PathVariable("date") int date, Model model) {
long userId = this.getCurrentUserId(); // just a method to get the user id requesting the task
List<Task> taskList = taskService.findByDateAndUserId(date, userId);
model.addAttribute("taskList", taskList);
return "/calendar";
}
calendar.html看起來像這樣(我只粘貼相關部分)
<html lang='en' xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset='utf-8' />
<title>Dashboard</title>
<link rel="stylesheet" href="../static/css/calendar.css">
<script src="jquery-3.5.1.min.js"></script>
</head>
<body>
<ul style="list-style-type: none; margin: 0;">
<li><a th:href="@{/calendar/20220228}">show</a></li>
<li><a th:href="@{/calendar/20220301}">show</a></li>
<li><a th:href="@{/calendar/20220301}">show</a></li>
</ul>
......
<div th:each="task : ${taskList}">
<label th:text="${task.name}"></label>
</div>
<!-- the rest is irrelevant to the question --!>
.......
</html>
所以每當我點擊<a>元素時,客戶端都會向服務器發送一個請求,并且 URL 由 GetMapping 方法處理,回傳任務。但是在發生這種情況時,頁面也會重繪 。有沒有辦法在不重繪 頁面的情況下顯示任務?
我嘗試從 display 方法回傳 void ,但 Spring 最終自動回傳/calendar/{date},它仍然不是我想要的
@GetMapping("/calendar/{date}")
public void displayTasksByClick(@PathVariable("date") int date, Model model) {
long userId = this.getCurrentUserId(); // just a method to get the user id requesting the task
List<Task> taskList = taskService.findByDateAndUserId(date, userId);
model.addAttribute("taskList", taskList);
}
uj5u.com熱心網友回復:
對于您當前的實施,不,這是不可能的。您必須重繪 頁面才能顯示任務。這就是服務器端渲染的作業方式。使用動態資料在服務器上創建頁面,然后將靜態頁面回傳給瀏覽器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/450863.html
上一篇:SpringSecurityUserDetailsS??ervice拋出UsernameNotFoundException
