【发布时间】:2018-04-18 07:46:17
【问题描述】:
我编写了一个简单的应用程序,让我们可以编写、更新和删除帖子。到目前为止,我可以编写和更新,但我的删除命令似乎不起作用。这是我的jsp代码:
<table>
<tr>
<th>Number</th>
<th>Title</th>
<th>Author</th>
<th></th>
</tr>
<c:forEach var="post" items="${listOfPosts}">
<tr>
<td>${post.id}</td>
<td>${post.title}</td>
<td>${post.author}</td>
<td>
<a href="<c:url value="/editPost/${post.id}" />" >Edit</a>
<form:form action="deletePost" method="post" commandName="deletedBlogpost">
<input type="submit" value="Delete post">
</form:form>
</td>
</tr>
</c:forEach>
</table>
和我的控制器方法,我尝试在其中实现 Post-Redirect-Get 模式:
@RequestMapping(value = "deletePost", method = RequestMethod.POST)
public String deletePostPOST (@ModelAttribute("deletedBlogpost") BlogPost blogpost, BindingResult bindingResult, RedirectAttributes ra) {
if (bindingResult.hasErrors()) {
return ERROR_PAGE_VIEW;
}
repo.delete(blogpost);
ra.addFlashAttribute("blogpost", blogpost);
return "redirect:/delete-success";
}
@RequestMapping(value = "delete-success", method = RequestMethod.GET)
public String deletePostGET(@ModelAttribute("deletedBlogpost") BlogPost blogpost, Model model){
model.addAttribute(M_LIST_OF_POSTS, repo.findAll());
return RESULT_PAGE_VIEW;
}
我想它必须是jsp形式的东西,因为我的代码甚至没有到达控制器的代码。任何帮助将不胜感激,因为我仍然是这里的初学者,我仍在努力学习基础知识。
编辑这是我的实体 BlogPost
@Entity
@Table(name="posts")
public class BlogPost {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name="post_title")
private String title;
@Column(name="post_content")
private String content;
@Column(name="post_author", nullable = false)
private String author;
public BlogPost(){}
public BlogPost(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String toString(){
return "ID: "+getId()+", tytul: "+getTitle()+", tresc: "+getContent()+", autor: "+getAuthor()+"\n";
}
}
还有我可以建立 PostgreSQL 连接的 application.properties:
#PostgreSQL
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/blogdatabase
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
编辑: 我试图通过添加到jsp表单来解决它
<input name="id" type="hidden" value="${post.id}"/>
并通过添加到 deletePostPost 方法
repo.delete(deletedBlogpost.getId());
什么都没有改变。单击按钮不会调用任何操作。
【问题讨论】:
-
您需要发送帖子的ID才能删除。您的表单目前根本没有发送任何内容。所以你的服务器不可能知道要删除哪个帖子。使用隐藏的输入字段。无需发送整个博客帖子信息即可将其删除。身份证就够了。
-
另外,“我的删除命令似乎不起作用”太含糊了。您必须学会比这更好地诊断问题:使用调试器查看调用了哪个方法,变量值是什么。将日志记录语句添加到代码中。使用其开发工具等检查浏览器发送的请求和响应。
-
尝试像 /deletePost 这样的操作。它应该工作。然后按照@JBNizet 的建议传递帖子的 ID,以便服务器知道需要删除哪个帖子。由于这些是基于 Rest 的调用,我建议您使用正确的 HttpMethod 方法(即在本例中为 DELETE)。
-
您的
-
@Vino 有多个表单不会造成任何问题,也不需要 JavaScript 提交 POST 请求。
标签: java spring jsp controller