使用 MySQL 从 Spark Framework 和 Thymeleaf 中的数据库中删除用户

Deleting user from database in Spark Framework and Thymeleaf with MySQL

我已经用 MySQL 数据库设置了 spark 框架和 thymeleaf。我无法将要删除的用户 ID 传递到我的应用程序后端。

后端

get("/delete", (rq, rs) -> {
        map.put("lista", studentService.getAllStudents());
        return thymeleafTemplateEngine.render(new ModelAndView(map,"delete"));});

delete("/delete:id", (rq, rs) -> {
           studentService.deleteStudent(rq.params(":id"));
           return "Korisnik uspesno izbrisan iz baze.";
        });

服务

public void deleteStudent(String id){
    sql = "DELETE FROM student WHERE student_id="+id;
    con = ConnectionManager.getConnection();
    try{
        stmt = con.createStatement();
        stmt.executeUpdate(sql);
    } catch (SQLException e){
        e.printStackTrace();
    }
}

前端

<form th:method="delete">
  <input th:name="id" type="text" placeholder="Id korisnika">
  <button type="submit">Submit</button>
</form>

这是从 thymeleaf 模板 http://localhost:4567/delete?id=15 传递过来的 url,我希望它通过 spark frameworks delete 方法注册,有什么方法可以实现吗?

后端

delete("/delete/deleteStudent", (rq, rs) -> {
       studentService.deleteStudent(rq.queryParams("id"));
       map.put("lista", studentService.getAllStudents());
       return thymeleafTemplateEngine.render(new ModelAndView(map, "delete"));
    });

服务

public void deleteStudent(String id){
    System.out.println("Delete Called");
    sql = "DELETE FROM student WHERE student_id="+id;
    con = ConnectionManager.getConnection();
    try{
        stmt = con.createStatement();
        stmt.executeUpdate(sql);
    } catch (SQLException e){
        e.printStackTrace();
    }
}

前端

<form th:method="delete" th:action="@{delete/deleteStudent}">
    <input type="number" th:name="id">
    <button type="submit">Delete user</button>
</form>

问题是我不知道如果你想在你的浏览器中发出删除请求你需要一个 XMLHttpRequest,所以当我安装 Firefox 的 RESTClient 插件并向我的服务器发送删除请求时,一切正常好的。