我是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.
答案 0 :(得分:-1)
请求映射路径对于2种以上的方法不能相同。使用类似这样的东西
df = pd.pivot(index=df.name, columns=df.day, values=df.quantity)
print (df.Sunday.sub(df.Monday).reset_index(name='sub_quantity'))
name sub_quantity
0 A 9.0
1 B 28.0