0

I was performing a CRUD operation, when tried to delete an item using POST method it gave an error Request method 'POST' not supported

Here is a form which performs delete : adminLogin.jsp

<form:form method="delete" action="deleteMenuItems/${itemName}" modelAttribute="menuItems">
       Item name <input type="text" name="itemName"></br>
       <input type="submit" value="delete">
</form:form>

AdminPageController.java

@RequestMapping(value="/deleteMenuItems/{itemName}", method=RequestMethod.DELETE)
public ModelAndView deleteMenuItem(@PathVariable("itemName") String itemName){
    this.menuItemsDao.deleteItems(itemName);
    return new ModelAndView("adminLogin");

}

MenuItemDao.java

public void deleteItems(String itemName) {
    Session session = sessionFactory.openSession();
    menuItems = (MenuItems) session.load(MenuItems.class, new String(itemName));
    session.beginTransaction();
    session.delete(menuItems);
    session.getTransaction().commit();
}

Also when I restart server everytime the previous vaule from db is cleaned and new table is formed, what might be the configuration in hibernate to solve this?

the problem might be simple but I am new to spring hope you will help me, thank you

  • `Request method 'POST' not supported` this is an error from the java console right?? – Kenry Sanchez Nov 22 '17 at 03:53
  • Possible duplicate of [Using PUT and DELETE methods in Spring MVC](https://stackoverflow.com/questions/13629653/using-put-and-delete-methods-in-spring-mvc) – Vasan Nov 22 '17 at 03:59
  • @KenrySanchez Yes that's the error in java console –  Nov 22 '17 at 07:47
  • is not better to change `RequestMethod.DELETE` to a POST method?? You're using DELETE, maybe it is badly implemented. – Kenry Sanchez Nov 22 '17 at 13:53
  • @KenrySanchez i have tried using get post nothing helped –  Nov 22 '17 at 15:37
  • Changing to Post and tell me what's the message console – Kenry Sanchez Nov 22 '17 at 16:39
  • DispatcherServlet with name 'spring' processing POST request for [/restroo/admin/deleteMenuItems/] Looking up handler method for path /admin/deleteMenuItems/ Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported Request method 'POST' not supported Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling Successfully completed request This is the error @KenrySanchez –  Nov 23 '17 at 02:53
  • now with GET it is not giiving error but the task to delete is not being performed i.e items are not being deleted –  Nov 23 '17 at 04:14
  • @RequestMapping(value="deleteMenuItems/{itemName}", method=RequestMethod.DELETE) – Kenry Sanchez Nov 23 '17 at 05:26
  • Can you show what is inside of `ModelAndView`? – Kenry Sanchez Nov 23 '17 at 05:29
  • Sorry didn't get. What should I send model and view separately ? –  Nov 23 '17 at 06:18

1 Answers1

0

The current HTML5 draft does not support PUT or DELETE in forms. you have two options: 1 . change the form to post and also your method in function in java 2. use ajax to send the form using delete method

Juan Pablo
  • 304
  • 3
  • 8
  • changing that also didn't help giving the same error –  Nov 22 '17 at 08:02
  • https://stackoverflow.com/questions/15699350/spring-request-method-post-not-supported, you have a nice example here, review the method in java and the sintax in your action.. seems there are two // – Juan Pablo Nov 22 '17 at 08:26