【发布时间】: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