Java Spring说请求方法';删除';尝试删除数据库项时不受支持

Java Spring说请求方法';删除';尝试删除数据库项时不受支持,java,spring,jpa,Java,Spring,Jpa,我正在进行一个学校项目,在该项目中,您可以将Todo项添加到数据库中,显示它们,并授予用户访问为每个项创建的各个页面的权限。我正在使用Spring和我的删除按钮,显示特定任务的各个页面也不起作用。在这个项目中,我们使用JPA来处理数据库 以下是我的TodoItemDatabaseController: package wad.tododatabase; import org.springframework.beans.factory.annotation.Autowired; import o

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

以下是我的TodoItemDatabaseController:

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>
这是我的TodoRepository界面:

package wad.tododatabase;

/**
 *
 * @author riku
 * 
 */
import org.springframework.data.jpa.repository.JpaRepository;

public interface TodoRepository extends JpaRepository<TodoItem, Long> {


}
package wad.tododatabase;
/**
*
*@作者riku
* 
*/
导入org.springframework.data.jpa.repository.JpaRepository;
到存储库的公共接口扩展了JpaRepository{
}

有没有关于如何完成这项工作的建议?

请尝试使用此adnotation:

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

请尝试使用此adnotation:

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

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