【问题标题】:Why deleteCustomer function in Controller class is not executed when link is clicked?为什么单击链接时不执行Controller类中的deleteCustomer函数?
【发布时间】:2020-08-25 02:34:05
【问题描述】:

以下函数在CustomerController.java

中定义
    @GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("customerId") int theId, Model theModel) {
    
    // get the customer from our service
    Customer theCustomer = customerService.getCustomer(theId);
    
    // set customer as a model attribute to pre-populate the form
    theModel.addAttribute("customer", theCustomer);
    
    // send over to out form
    return "customer-form";
}

@GetMapping("/delete")
public String deleteCustomer(@RequestParam("customerId") int theId) {
    
    // delete the customer
    customerService.deleteCustomer(theId);
    
    return "redirect:/customer/list";
}

list-customer.jsp 文件如下。

<!--  loop over and print out customers -->
            <c:forEach var="tempCustomer" items="${customers}">
            
                <!--  construct an "update" link with customer id -->
                <c:url var="updateLink" value="/customer/showFormForUpdate">
                    <c:param name="customerId" value="${tempCustomer.id}" />
                </c:url>
                
                <!--  construct an "delete" link with customer id -->
                <c:url var="deleteLink" value="/customer/delete">
                    <c:param name="customerId" value="${tempCustomer.id}" />
                </c:url>
                
                <tr>
                    <td> ${tempCustomer.firstName} </td>
                    <td> ${tempCustomer.lastName} </td>
                    <td> ${tempCustomer.email} </td>
                    <td>
                        <!--  display the update link -->
                        <a href="${updateLink}">Update</a>
                        |
                        <a href="${deleteLink}"
                        onclick="if (!(confirm('Are you sure you want to delete this customer?'))) return false">Delete</a>
                    </td>
                </tr>
            </c:forEach>

当我点击Update链接时,CustomerController.java中的showFormForUpdate函数被调用。 但是,当我单击 Delete 链接时,deleteCustomer 函数不会被调用,也不会显示任何 WARNING: No mapping for GET error。我在删除 onclick="if (!(confirm('Are you sure you want to delete this customer?'))) return false" 后尝试了这个功能。但问题依然存在。
有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您的删除 a href 链接正在被您的 onclick 函数覆盖。
  • 但是当我删除onclick功能时它仍然不起作用。
  • @shinjw 如果我将“deleteLink”替换为“updateLink”,页面将被重定向到其他页面。所以我认为 deleteLink 有一些问题。但我不知道它是什么。

标签: javascript html spring-mvc jsp


【解决方案1】:

最后,我解决了这个问题。我无法确定问题的原因,但我通过清理构建和重建解决了问题。

【讨论】:

    猜你喜欢
    • 2010-12-09
    • 2014-10-04
    • 2016-10-13
    • 1970-01-01
    • 2021-11-14
    • 2011-09-29
    • 2011-06-29
    • 2021-09-16
    • 1970-01-01
    相关资源
    最近更新 更多