有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpring从数据库和列表中删除记录

我编写了一个简单的应用程序,让我们可以编写、更新和删除帖子。到目前为止,我可以编写和更新,但我的delete命令似乎不起作用。以下是我的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>

还有我的控制器方法,我尝试在其中实现后重定向获取模式:

@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表单的东西,因为我的代码甚至还没有到达控制器的代码。任何帮助都会被感激,“因为我在这里还是一个初学者,我还在努力学习基础知识

编辑这是我的实体博客帖子

@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连接的属性:

#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());

一切都没有改变。单击按钮不会调用任何操作


共 (1) 个答案

  1. # 1 楼答案

    试一试

    你的仓库。deleteById(id)