Spring Mvc Controller - removal problem

i works in a j2ee project (pojo layer, Dao level (hibernation), service level (spring), view (spring mvc)) I have an article table after each line, I want to add a link to delete it.

this is my look

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

here is the controller to delete

 @RequestMapping(value="/articles/{articleId}", method=RequestMethod.POST) public String deleteContact(@PathVariable("articleId") Integer articleId) { articleService.removeArticle(articleId); return "redirect:/articles.html"; } 

this is the level of service

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

this is a Dao layer (I'm trying to find an article to delete it)

  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); } } 

but when I start the project and click on the delete link, I have a 404 Etat HTTP error 404 - / Spring3Hibernate / articles / 1 description The requested resource (/ Spring3Hibernate / articles / 1) is not available

Can someone help me?

+2
source share
2 answers
  <td><a href="articles/${article.articleId}">delete</a></td> 

This is a standard GET request, but your controller maps to POST.

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

In addition, it looks like a very big security issue. I can write a very simple 10-line program that will call get / post from / articles / 1 to / articles / {any number} and delete all your data. I recommend only taking this into account when developing such applications.

+5
source

Try the DELETE query method. The GET method is not recommended for anything that changes the value on the / db server. If you want to stick with the message, make it a submit form instead of href

 RequestMapping(value="/articles/{articleId}", method=RequestMethod.DELETE) 
0
source

Source: https://habr.com/ru/post/900142/


All Articles