【问题标题】:Cannot update data from form submission in spring春季无法从表单提交中更新数据
【发布时间】:2017-04-12 06:47:00
【问题描述】:

我是java新手,这是我第一次使用spring框架。 我想通过表单提交更新数据库中的信息,但我认为数据未被正确接受?我正在使用 MySql 和 jdbcTemplate,包含模型、dao 和服务。

这是我的控制器

@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
public String editForm(@PathVariable("id") int id, ModelMap modelMap, 
        Model model) {
    model.addAttribute("post", new Post());
    modelMap.put("post", postService.find(id));
    return "post/edit";
}

@RequestMapping(value = "edit/{id}", method = RequestMethod.POST)
public String editFormSubmit(@PathVariable("id") int id, ModelMap modelMap, 
        @ModelAttribute Post post) {
    postService.update(post);
    return "result/edit_post";
}

这是我的 DAO 类

@Override
public void update(Post post) {
    String query = "UPDATE post SET questioner=?, answering=?, question=?, "
            + "answer=?, date=?, status=? WHERE id=?";
    jdbcTemplate.update(query, post.getQuestioner(), post.getAnswering(), post.getQuestion(), 
            post.getAnswer(), post.getDate(), post.isStatus(), post.getId());
}

这是我的模型类(如果有帮助的话)

private int id;
private boolean status;
private String questioner, answering, question, answer, date;

public Post() {
    super();
}

public Post(int id, boolean status, String questioner, String answering, 
        String question, String answer, String date) {
    super();
    this.id = id;
    this.status = status;
    this.questioner = questioner;
    this.answering = answering;
    this.question = question;
    this.answer = answer;
    this.date = date;
}

// getters and setters here ...

这是我的服务

@Override
public void update(Post post) {
    this.postDao.update(post);
}

另外,这是我正在使用的edit.html

<form action="#" th:action="@{/post/edit/${post.id}}" th:object="${post}" method="post">
ID: <input type="text" th:field="*{id}" readonly="readonly" /><br/>
Questioner: <input type="text" th:field="*{questioner}" /><br/>
Answering: <input type="text" th:field="*{answering}" /><br/>
Question: <input type="text" th:field="*{question}" /><br/>
Answer: <input type="text" th:field="*{answer}" /><br/>
Date: <input type="text" th:field="*{date}" /><br/>
Status: <input type="number" th:field="*{status}" min="0" max="1" /><br/>
<input type="submit" value="Submit" /> 
</form>

问题是,我不知道我哪里出错了。我为表用户做了完全相同的事情,我可以进行 crud 操作并且它工作得很好,但是当我为表帖子复制它时,我只能进行插入和删除,而更新只会给出错误。错误说:

HTTP Status 400 - description The request sent by the client was syntactically incorrect.

【问题讨论】:

  • 您使用的表单/客户端在哪里。
  • @M.Deinum 我已经更新了帖子并将表格包含在问题中
  • 您的表单提交给/post/update/,但是您的控制器映射为/post/edit
  • @M.Deinum 我在尝试 Avinash 建议时忘记改回它,但即使是 /post/edit 它仍然会收到错误

标签: java mysql spring spring-mvc error-handling


【解决方案1】:

请求映射路径不能超过 2 个方法。使用类似的东西

@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
public String editForm(@PathVariable("id") int id, ModelMap modelMap, 
        Model model) {
    model.addAttribute("post", new Post());
    modelMap.put("post", postService.find(id));
    return "post/edit";
}

@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
public String editFormSubmit(@PathVariable("id") int id, ModelMap modelMap, 
        @ModelAttribute Post post) {
    postService.update(post);
    return "result/edit_post";
}

【讨论】:

  • @Alvinash 我按照你的建议做了并将 POST 方法更改为更新,我还将 edit.html 中的表单操作更改为 &lt;form action="#" th:action="@{/post/update/${post.id}}" th:object="${post}" method="post"&gt; 但它仍然返回 HTTP 状态 400
  • 映射不一样...映射的一切都考虑在内,url、方法、参数等。事实上,整个组合只能存在一次,但对于同一个 URL,您完全可以有两种方法(一种 GET 和一种 POST)。
  • 恐怕我听不懂?我在顶部使用@Controller @RequestMapping("post")?所以/post/update/${post.id} 的POST 方法是editFormSubmit 对吗?我正在使用@RequestMapping(value = "edit/{id}", method = RequestMethod.POST) public String editFormSubmit()
猜你喜欢
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 2018-07-31
  • 2015-01-19
  • 2015-01-03
  • 2015-05-30
相关资源
最近更新 更多