Spring - 从DB和列表中删除记录

时间:2017-11-06 07:42:15

标签: java spring jsp controller

我编写简单的应用程序,让我们编写,更新和删除帖子。到目前为止,我可以编写和更新,但我的删除命令似乎无法正常工作。这是我的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 Pattern:

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

}

和我的application.properties,我可以建立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 个答案:

答案 0 :(得分:0)

尝试

yourRepository.deleteById(id)