Home > Enterprise >  Is it the right way to send a request with DELETE method?
Is it the right way to send a request with DELETE method?

Time:12-04

    <form action="board" method="post">
        <input type="hidden" name="_method" value="delete">
        <input type="hidden" name="board_no" value="<c:out value='${board.board_no }' />">
        <input type="submit" value="Delete message">
    </form>

I'm trying to send a request with DELETE method by using a form tag as above. But I kept failing to get a right result.

The code above results in sending with POST method, not DELETE.

However, the code below which the value 'delete' is replaced by 'put' works perfectly.

    <form action="board" method="post">
        <input type="hidden" name="_method" value="put">

    ...

    </form>

I'm wondering if using input tag with "delete" value is not the right way to send a request with Delete method.


Java Controller:

    @PostMapping("/board")
    public String insertBoard2(Board board) {
        service.insertOneBoard(board);
        return "redirect:/boardList";
    }
    
    @PutMapping("/board")
    public String updateBoard2(Board board) {
        service.updateOneBoard(board);
        return "redirect:/board?board_no="   board.getBoard_no();
    }
    
    @DeleteMapping("/board")
    public String deleteBoard(Board board) {
        service.deleteOneBoard(board.getBoard_no());
        return "redirect:/boardList";
    }

Error message when the form submitted:

org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: java.sql.SQLIntegrityConstraintViolationException: (conn=2090) Column 'board_title' cannot be null
### The error may exist in file [C:\Users\***\eclipse-workspace\RestfulBBS-Spring_Framework-MyBatis\target\classes\BoardMapper.xml]
### The error may involve boardMapper.insertOne-Inline
### The error occurred while setting parameters
### SQL: insert into board(board_no, member_id, board_title, board_content, board_hit, write_date, is_notice)     values (default, ?, ?, ?, default, now(), ?)
### Cause: java.sql.SQLIntegrityConstraintViolationException: (conn=2090) Column 'board_title' cannot be null

The error message shows that the request is sent with POST method.(the required parameters for insert is not saisfied.)

CodePudding user response:

I solved my problem adding filter to web.xml as below.

<filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

Using PUT, DELETE method in Spring framework needed additional filter.

CodePudding user response:

    <form action="board" method="post">

This line refers to the method that is being called in your back-end when a request from the form is sent to the servlet.

You are then assuming this line:

<input type="hidden" name="_method" value="delete">

Generates the delete method. But it does not. This can be: because no delete method is defined as such in your Java-code.

Please do post the method for the post within your Java-code, as we cannot debug further now.

I'm going to assume the following: The post method is setup in your application, it succesfully calls the servlet in spring, but then has internally no difference between put and delete.

  • Related