Spring-MVC应用程序中的Javascript确认消息

Sou*_*uad 0 javascript spring-mvc

我想在删除应用程序中的任何元素之前显示确认消息,我有:

<form:form name="formule" method="get" commandName="user"
    onsubmit="confirmDelete('${pageContext.request.contextPath}/delete_element')">       
     <form:input type="hidden" path="id" name="id" />
     <input type="submit" value="DELETE" />
</form:form>
Run Code Online (Sandbox Code Playgroud)

功能 :

function confirmDelete(delUrl) {
    if (confirm("Are you sure ?")) {
        document.location = delUrl;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是控制器:

@RequestMapping(value = "/delete_element", method = RequestMethod.GET)
public String getInfo(@ModelAttribute User user,@RequestParam("id") int id,ModelMap model) {
    userservice.DeleteUser(id);
    return "ViewName";
}
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:函数的链接没有把我带到控制器所以没有发生任何事情,我们可以说javascript发送的请求没有到达服务器 ..是否真的很难在Spring MVC中集成任何东西像那样 ?!

Pav*_*ral 5

为什么需要篡改表单操作URL?

<c:url var="deleteUrl" value="/delete_element" />
<form:form method="get" commandName="user" action="${deleteUrl}"
         onsubmit="return confirm('Are you sure?') ? true : false;">
Run Code Online (Sandbox Code Playgroud)

另外你为什么需要绑定@ModelAttribute和/或ModelMap?您没有发送或设置任何用户数据,因此您可能只是忽略:

@RequestMapping(value = "delete_element", method = RequestMethod.GET)
public String getInfo(@RequestParam("id") int id) {
Run Code Online (Sandbox Code Playgroud)

还有一点偏离主题:你不应该使用 HTTP GET方法来修改请求. http://www.w3.org/TR/REC-html40/interact/forms.html#submit-format