hibernate - Spring Mvc Controller - 删除问题

标签 hibernate spring-mvc controller dao

我在一个j2ee项目中工作(pojo层,Dao层(hibernate),服务层(spring),View(spring mvc)) 我在每一行后面都有一个文章表,我想添加一个链接来删除它。

这是我的观点

<c:if test="${!empty articles}">
<table>
    <tr>
        <th>Article ID</th>
        <th>Article Name</th>
        <th>Article Desc</th>
        <th>Added Date</th>
        <th>operation</th>
    </tr>

    <c:forEach items="${articles}" var="article">
        <tr>
            <td><c:out value="${article.articleId}"/></td>
            <td><c:out value="${article.articleName}"/></td>
            <td><c:out value="${article.articleDesc}"/></td>
            <td><c:out value="${article.addedDate}"/></td>
            <td><a href="articles/${article.articleId}">delete</a></td>
        </tr>
    </c:forEach>
</table>

这是要删除的 Controller

@RequestMapping(value="/articles/{articleId}", method=RequestMethod.POST)
public String deleteContact(@PathVariable("articleId")
Integer articleId) {

    articleService.removeArticle(articleId);

    return "redirect:/articles.html";
}

这是服务层

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void removeArticle(Integer id) {
    articleDao.removeArticle(id);
}

这是 Dao 层(我尝试找到文章然后将其删除)

    public void removeArticle(Integer id) {
            //to get the article
    Article article = (Article) sessionFactory.getCurrentSession().load(
            Article.class, id);
    if (null != article) {
        sessionFactory.getCurrentSession().delete(article);
    }

}

但是当我运行项目并单击删除链接时,出现 404 错误 HTTP 404 -/Spring3Hibernate/articles/1 描述 请求的资源(/Spring3Hibernate/articles/1)不可用

有人可以帮助我吗?

最佳答案

 <td><a href="articles/${article.articleId}">delete</a></td>

这是标准 GET 请求,但您的 Controller 映射到 POST。

@RequestMapping(value="/articles/{articleId}", method=RequestMethod.POST)

此外,这看起来是一个非常大的安全问题。我可以编写非常简单的 10 行程序,该程序将使用 get 或 post 请求调用从/articles/1 到/articles/{any number} 并删除整个数据。我建议在设计此类应用程序时考虑到这一点。

关于hibernate - Spring Mvc Controller - 删除问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5133564/

相关文章:

java - 无法将数据插入到数据库 mysql 中的我的表中

hibernate OneToMany 列表排序持续但反转?

java - Spring Boot w/JSP - 找不到 CSS 文件

java - Spring 启动+ Spring 数据休息: post json null value in column "first_name" violates not-null constraint

Hibernate 3.0 + 缺少 ElementCollection 类

tomcat - 部署项目war时出现Tomcat错误

java - Spring Controller 在 10 秒后做一些工作

ruby-on-rails - 如何证明 Rails 上 Controller 的简单方法?

php - Zend Framework 和防止胖 Controller

grails - 如何在 grails 中从 Controller 调用服务