Java 如何处理ajax确认对话框的是或否

Java 如何处理ajax确认对话框的是或否,java,ajax,spring,spring-mvc,Java,Ajax,Spring,Spring Mvc,在belwo代码中,当用户访问以下url时,我尝试创建ajax调用,如下所示: "/product/remove/" + idx, 我想显示一个带有“Yes”和“No”的对话框,只有当用户输入上一个url并单击enter,并且在处理删除或删除操作的控制器逻辑执行之前,此对话框才会出现,这就是我在下面的$.ajax调用中使用“beforeSend”属性的原因 我在谷歌上搜索了几篇关于如何将确认对话框与ajax调用和spring MVC集成并创建的帖子,但是我得到的大多数信息需要进一步澄清

在belwo代码中,当用户访问以下url时,我尝试创建ajax调用,如下所示:

    "/product/remove/" + idx,
我想显示一个带有“Yes”和“No”的对话框,只有当用户输入上一个url并单击enter,并且在处理删除或删除操作的控制器逻辑执行之前,此对话框才会出现,这就是我在下面的$.ajax调用中使用“beforeSend”属性的原因

我在谷歌上搜索了几篇关于如何将确认对话框与ajax调用和spring MVC集成并创建的帖子,但是我得到的大多数信息需要进一步澄清

我想要实现的是,当用户单击Yes时,代码belwo中显示的控制器应该正常执行。当用户单击“否”时,不会发生任何事情,只会显示对话框 应该消失

请在下面找到我的尝试,并帮助我实现它

代码_1

@<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ajax confirm delete prodcut</title>
<script
src="${pageContext.request.contextPath }/resources/js/jquery-1.6.2.js"></script>

<script type="text/javascript">

$(document).ready(function() {


$('#confirmremoveform').click(function() {
var idx = $('#idx').val();
var ans = confirm("Are you sure you want to delete this Record?");
if (ans) {
    $.ajax({
        type: "DELETE",
        url: "/product/remove/" + idx,
        dataType : 'json',
        contentType : 'application/json',
        beforeSend: function () {
            $("#modal-book").modal("show");
          },
          success: function (data) {
            $("#modal-book .modal-content").html(data.html_form);
          },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
}
});
}
</script>

</head>

<body>
<fieldset>
    <legend>confirmremove</legend>
    <input type="button" value="confirmremove" id="confirmremoveform" />
    <br/>
    <span id="result0"></span>
    </fieldset>
</body>

</html>
@
Ajax确认删除prodcut
$(文档).ready(函数(){
$('confirmremoveform')。单击(函数(){
var idx=$('#idx').val();
var ans=confirm(“您确定要删除此记录吗?”);
如果(ans){
$.ajax({
键入:“删除”,
url:“/product/remove/”+idx,
数据类型:“json”,
contentType:'应用程序/json',
beforeSend:函数(){
美元(“#模态书”)。模态(“显示”);
},
成功:功能(数据){
$(“#modal book.modal content”).html(data.html_form);
},
错误:函数(错误消息){
警报(errormessage.responseText);
}
});
}
});
}
确认删除

控制器

@Controller
@RequestMapping("/product/remove")
public class RemoveProductPageController {

public final static String sRemoveProductFromListAttributeName = "removeProductFromList";

public final static String CONTROLLER_URL = "/product/remove";
public final static String DO_REMOVE_HANDLER_METHOD_URL = CONTROLLER_URL + "/{idx}";

@Autowired
private ProductService productService;

@RequestMapping(value = "/{idx}", 
        method = RequestMethod.DELETE)
@ResponseBody
public ResponseEntity<String> doRemove(@Validated @Size(min = 0) @PathVariable(required = true) int idx,
        Model model) {

    Product productToBeRemove = productService.getProductFromListByIdx(idx);
    if (productToBeRemove == null) {
        return new ResponseEntity<String>("no product is avaialble at index:" + idx, HttpStatus.NOT_FOUND);
    }

    model.addAttribute(RemoveProductPageController.sRemoveProductFromListAttributeName, productToBeRemove);
    productService.removeProdcutFromListBxIdx(idx);
    return new ResponseEntity<String>("product removed from index: " + idx, HttpStatus.OK);
}
}
@控制器
@请求映射(“/product/remove”)
公共类RemoveProductPageController{
公共最终静态字符串sRemoveProductFromListAttributeName=“removeProductFromList”;
公共最终静态字符串控制器_URL=“/product/remove”;
公共最终静态字符串DO_REMOVE_HANDLER_METHOD_URL=CONTROLLER_URL+“/{idx}”;
@自动连线
私人产品服务;
@RequestMapping(value=“/{idx}”,
方法=RequestMethod.DELETE)
@应答器
公共响应性doRemove(@Validated@Size(min=0)@PathVariable(required=true)int idx,
(模型){
Product productToBeRemove=productService.getProductFromListByIdx(idx);
if(productToBeRemove==null){
返回新的响应属性(“索引:+idx,HttpStatus.未找到任何产品可用”);
}
model.addAttribute(RemoveProductPageController.sRemoveProductFromListAttributeName,productToBeRemove);
productService.removeProdcutFromListBxIdx(idx);
返回新的响应属性(“从索引中删除的产品:+idx,HttpStatus.OK”);
}
}

替换以下逻辑

var ans = confirm("Are you sure you want to delete this Record?");
if (ans) {
    //your all code
}

// with a single liner
if (confirm("Are you sure you want to delete this Record?")) {}

替换以下逻辑

var ans = confirm("Are you sure you want to delete this Record?");
if (ans) {
    //your all code
}

// with a single liner
if (confirm("Are you sure you want to delete this Record?")) {}

thaks..但我想在单击YES的情况下..控制器继续正常执行,在单击No的情况下,什么都不会发生,只有dislaog消失..这是一个有趣的情况,你的意思是确认弹出窗口没有消失,或者这也会删除吗?这真的很奇怪,我在这一点上没有理解你。嗨..你能看看这个吗estion:这几乎是同一个主题。我将删除此postthaks。但我希望在单击“是”的情况下。控制器继续正常执行,在单击“否”的情况下,什么都不会发生,只是dislaog消失了。这是一个有趣的情况,你的意思是确认弹出窗口没有消失,还是也会删除?我不明白这真的很奇怪你好,请看一看这个问题:这几乎是同一个话题。我将删除这篇文章