有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

postgresql使用Spark/Java中Velocity/HTML表单中的PUT http方法

utI有一个Spark/Velocity模板应用程序,我正在尝试使用PUT路由来遵循良好的REST约定来更新数据库中的记录

在我的HTML表单中,我获得了更新postgres DB中记录所需的信息:

<form action="/tasks/$editTask.getId()">
  <input id="description" name="description" type="text" value="$editTask.getDescription()">
  <input type="date" name="duedate" value="$editTask.getDueDate()">
  <button type="submit">Edit task</button>
</form>

但我对如何在我的应用程序中捕捉到这一点感到困惑。JAVA如果我改变方法method="PUT",什么都不会发生。以下是我的推杆路线:

post("/tasks/:id", (request,response) -> {
    HashMap<String, Object> model = new HashMap<String, Object>();
    int id = Integer.parseInt(request.params("id"));
    Task editTask = Task.find(id);
    String newDescription = request.queryParams("description");
    String newDueDate = request.queryParams("duedate");

    editTask.update(newDescription, newDueDate);
    model.put("editTask", editTask);
    model.put("template", "templates/tasks.vtl");
    return new ModelAndView(model, layout);
}, new VelocityTemplateEngine());

我已尝试添加一个隐藏的输入字段:

<input type="hidden" name="_method" value="put">到我的表单,但它没有任何作用。当我尝试访问put路由时,没有出现任何错误,只是什么都没有发生。我已经测试了我的代码来更新POST路线上的记录,它工作得很顺利

非常感谢您就如何使用PUT提供的任何指导 我想我会有同样的问题使用删除等


共 (0) 个答案