gpt4 book ai didi

java - 在 Spring 无法从表单提交中更新数据

转载 作者:行者123 更新时间:2023-12-03 09:01:56 24 4
gpt4 key购买 nike

我是Java的新手,这是我第一次使用spring框架。
我想从表单提交中更新数据库中的信息,但是我认为数据没有被正确接受?我正在使用MySql和jdbcTemplate,其中包含模型,Dao和服务。

这是我的 Controller

@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.

最佳答案

两种以上方法的请求映射路径不能相同。使用这样的东西

@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";
}

关于java - 在 Spring 无法从表单提交中更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40846120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com