Home > Enterprise >  How to solve deleteById in springboot?
How to solve deleteById in springboot?

Time:12-02

CustomerController.java

@RequestMapping(value="/customer/delete",method = {RequestMethod.DELETE,RequestMethod.GET})
public String deleteCustomer(Long id) {
    
    customerService.deleteCustomer(id);
    return "redirect:/customer";
    
}

CustomerService.java

public void deleteCustomer(Long id) {
customerRepository.deleteById(id);      
    }

customer.html delete modal

<!--Delete Modal -->
<a th:href="@{/customer/delete/(id=${customer.id})}" 
        type="button"  id="deleteButton" 
        data-toggle="modal"
        data-target="#deleteModal">
        <span >delete_outline</span></a>

        <div  id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
            aria-hidden="true">
            <div  role="document">
                <div >
                    <div >
                        <h5  id="exampleModalLabel">Confirm Delete</h5>

                    </div>
                    <div >
                        Are you sure want to delete this customer record?
                    </div>
                    <div >
                        <a type="button" id="confirmDeleteButton" href="" >Yes, Delete</a>
                        <button type="button"  data-dismiss="modal">Cancel</button>

                    </div>
                </div>
            </div>
        </div>

js script

$('#deleteButton').on('click',function(event){
    var href = $(this).attr('href');
    $('#confirmDeleteButton').attr('href',href);
    $('#deleteModal').submit();
    
})

I am getting an error on clicking on delete confirmation button on modal

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Not Found, status=404). No message available

CodePudding user response:

Missing id in RequestMapping?

@RequestMapping(value="/customer/delete/{id}", ....

CodePudding user response:

CustomerController.java

@RequestMapping(value="/customer/delete/",method = {RequestMethod.DELETE,RequestMethod.GET})
public String deleteCustomer(Long id) {
    
    customerService.deleteCustomer(id);
    return "redirect:/customer";
    
}

The forward slash after the delete solved everything

  • Related