我使用 SpringBoot 創建了一個 CRUD 應用程式。我使用 PostgreSQL 作為資料庫。我的應用程式也使用 SpringSecurity。顯示和創建物件的方法完美運行。但是由于某種原因,更新和洗掉相同的物件會產生錯誤:
2022-11-01 08:51:14.602 WARN 12149 --- [io-8080-exec-10] .wsmsDefaultHandlerExceptionResolver:已解決 [org.springframework.web.HttpRequestMethodNotSupportedException:不支持請求方法“POST”]
我認為問題出在html代碼中。我用百里香葉。
**我的學生控制器:**
package ru.connor.FirstSecurityApp.controllers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.connor.FirstSecurityApp.model.Student;
import ru.connor.FirstSecurityApp.services.StudentService;
import javax.validation.Valid;
import java.util.Optional;
@Controller
@RequestMapping("/students")
@RequiredArgsConstructor
public class StudentController {
private final StudentService studentService;
@GetMapping()
public String showAllClasses(Model model) {
model.addAttribute("students", studentService.showAllStudent());
return "main/AllClasses";
}
@GetMapping("/{id}")
public String showById(@PathVariable("id") int id, Model model){
Optional<Student> student = Optional.ofNullable(studentService.showStudentById(id));
if (student.isEmpty()){
return "main/students/errorPage";
}else model.addAttribute("student", student);
return "main/students/index";
}
@GetMapping("/add")
public String addStudent(@ModelAttribute("student") Student student) {
return "main/students/new";
}
@PostMapping()
public String create(@ModelAttribute("student") @Valid Student student,
BindingResult bindingResult) {
if (bindingResult.hasErrors())
return "main/students/new";
studentService.addStudent(student);
return "redirect:/students";
}
@GetMapping("/{id}/edit")
public String edit(Model model, @PathVariable("id") int id) {
model.addAttribute("student", studentService.showStudentById(id));
return "main/students/edit";
}
@PatchMapping("/{id}")
public String update(@ModelAttribute("student") @Valid Student student, BindingResult bindingResult, @PathVariable("id") int id) {
if (bindingResult.hasErrors()){
return "main/students/edit";}
studentService.update(id, student);
return "redirect:/students";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable("id") int id){
Optional<Student> student = Optional.ofNullable(studentService.showStudentById(id));
if (student.isPresent()){
studentService.delete(id);
return "redirect:/students";
}
return "main/students/index";
}
}
學生服務:
package ru.connor.FirstSecurityApp.services;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.connor.FirstSecurityApp.model.Student;
import ru.connor.FirstSecurityApp.repository.StudentsRepository;
import java.util.List;
import java.util.Optional;
@Service
@RequiredArgsConstructor
@SuppressWarnings("unused")
@Transactional(readOnly = true)
public class StudentService {
private final StudentsRepository studentsRepository;
public List<Student> showAllStudent(){
return studentsRepository.findAll();
}
public Student showStudentById(int id){
Optional<Student> foundPerson = studentsRepository.findById(id);
return foundPerson.orElse(null);
}
@Transactional
public void addStudent(Student student){
studentsRepository.save(student);
}
@Transactional
public void update(int id, Student person){
person.setId(id);
studentsRepository.save(person);
}
@Transactional
public boolean delete(int id){
if (studentsRepository.findById(id).isPresent()){
studentsRepository.deleteById(id);
return true;
}
return false;
}
}
**指定洗掉表單的 HTML 視圖:**
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Student</title>
</head>
<body>
<h1 th:text="${student.get().getFullStudentName()}"></h1>
<hr>
<form method="post" th:action="@{/students/{id}(id=${student.get().getId()})}">
<input type="submit" value="Delete">
</form>
</body>
</html>
uj5u.com熱心網友回復:
正如您提到@PatchMapping的更新,那么您需要 HTTP PATCH方法而不是 POST。你使用過的洗掉也是@DeleteMapping如此,所以你需要使用 HTTP DELETE方法而不是 POST。
Create -> POST
Read -> GET
Update -> PUT/PATCH
Delete -> DELETE
通過表單提交,我認為 PATCH/PUT/DELETE 不起作用,因此在這種情況下,您需要更改@PatchMapping/@DeleteMapping并@PostMapping更新 URL,以便更新/洗掉它是唯一的。
PUT/PATCH/DELETE 僅適用于 REST API。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/525621.html
