Spring 说请求方法"DELETE"不支持,当尝试删除数据库项时



我正在做一个学校项目,在这个项目中,您将Todo项目添加到数据库中,显示它们,并授予用户访问为每个项目创建的各个页面的权限。我正在使用Spring,我的Delete按钮和显示特定任务的单个页面都不起作用。在这个项目中,我们使用JPA来处理数据库。

这是我今天的项目数据库控制器:

package wad.tododatabase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class TodoItemDatabaseController {
@Autowired
private TodoRepository todoRepository;
@GetMapping("/")
public String home(Model model) {
model.addAttribute
("items", this.todoRepository.findAll());
return "index";
}

这是不起作用的删除。

@DeleteMapping("/{itemId}")
public String remove(Model model, @PathVariable Long itemId) {
this.todoRepository.delete(this.todoRepository.getOne(itemId));
return "redirect:/";
}

@PostMapping("/")
public String post(@RequestParam String name) {
if (!name.trim().isEmpty()) {
TodoItem item = new TodoItem(name);
todoRepository.save(item);
}
return "redirect:/";
}

在这里,我试图获得特定的id,为项目创建单独的页面。

@GetMapping("/{id}")
public String findOne(Model model, @PathVariable Long id) {
TodoItem t1 = todoRepository.getOne(id);
t1.count();
model.addAttribute("item", t1);
return "item"; 
}

}

这是我的html文件。我发布了index.html文件的DELETE部分和item.html,这是单个待办事项的页面。

Index.html:

<table>
<tr>
<th>Task name</th>
<th></th>
</tr>
<tr th:each="item : ${items}">
<td><a th:href="@{/{item}(item=${item.id})}" th:text="${item.name}">Item name</a></td>
<td><form th:action="@{/{item}(item=${item.id})}" th:method="DELETE"><input type="submit" value="Done!"/></form></td>
</tr>
</table>

项目.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>TodoDatabase</title>
</head>
<body>
<h1 th:text="${item.name}">item name</h1>
<p>Item has been checked a total of <span th:text="${item.checked}">-1</span> times.</p>
<a th:href="@{/}">Back to listing.</a>
</body>
</html>

Spring也给出了2个警告,我想它们是警告:

2017-11-13 21:52:13.902  WARN 29312 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound             : Request method 'DELETE' not supported
2017-11-13 21:52:13.903  WARN 29312 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported

这是我的TodoRepository界面:

package wad.tododatabase;
/**
*
* @author riku
* 
*/
import org.springframework.data.jpa.repository.JpaRepository;
public interface TodoRepository extends JpaRepository<TodoItem, Long> {

}

有什么建议吗?

试试这个附件:

@RequestMapping(value = "/{itemId}"", method = RequestMethod.DELETE)
public @ResponseBody String remove(Model model, @PathVariable Long itemId) {
this.todoRepository.delete(this.todoRepository.getOne(itemId));
return "redirect:/";
}

最新更新