gpt4 book ai didi

java - 如何根据 ajax 弹出窗口或对话框的输入执行 Controller

转载 作者:行者123 更新时间:2023-12-02 00:35:47 25 4
gpt4 key购买 nike

在下面的代码“code_2”中,我有一个应用http方法DELETE的 Controller ,以根据提供的id删除特定产品。在“code_1”中,我试图创建ajax调用,在执行code_2中的 Controller 之前向用户显示或呈现一个对话框或弹出窗口,其中包含"is"和“否”。换句话说,当客户端调用时

/product/remove/2

他应该看到一个弹出窗口或对话框,其中包含"is"和“否”。当他单击"is"时,code_2 中的 Controller 应该正常执行。如果他单击“否”,则不会发生任何事情......只有对话框或弹出窗口消失。

对于弹出窗口或对话框,我执行了以下操作,如下所示:

<body>
<form action="http://www.google.com/search">
<input type="text" name="q" />
<input type="submit" value="Go" onclick="return confirm('Are you sure you want to search Google?')"/>
</form>
</body>

我找不到带有"is"和“否”按钮的表单...您能帮我更正它吗

我已经实现了以下代码_1和代码_2。

请让我知道代码是否正确或需要修改...我无法访问服务器而且,我是ajax和spring mvc技术的新手。

代码_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) {
$("#onsuccessfuldelete").text(data);
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
}
});
}

</script>

</head>

<body>
<form action="http://www.google.com/search">
<input type="text" name="q" />
<input type="submit" value="Go" onclick="return confirm('Are you sure you want to search Google?')"/>
</form>
</body>

</html>

代码_2

@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);
}
}

最佳答案

尝试使用SWAL (甜蜜警报)。它是 JavaScript 弹出窗口的可定制替代品。

有很多选项可供您选择。

示例:

JSP,单击删除按钮

$('#[idOfDeleteButton]').click(function (e) {
e.preventDefault();
swal({
title: 'Are you sure you want to delete this record',
type: 'warning',
showCancelButton: true
}).then(function () {
//execute once user confirms else nothing happens
var deleteLink = "[link to controller]?id="+[idTobeDeleted];
$.ajax({
url: deleteLink ,
contentType: "application/json",
type: 'GET',
success: function (res) {
//res is the string returned in the controller
if (res === "Success!") {
swal({
title: res,
text: "Delete Successful!",
type: 'success'
});

} else {
swal({
title: res,
text: "Deleting record Failed!",
type: 'error'
});
}
}
});
});
});

关于java - 如何根据 ajax 弹出窗口或对话框的输入执行 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57974702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com