Java SpringMVC:在进行AJAX调用后在对话框中显示数据
我不熟悉Spring和web技术 我有一个表,其中包含一个带有超链接的列。当我单击行的超链接时,我需要在对话框中显示该行的数据以及其他详细信息。我的控制器方法返回一个Java SpringMVC:在进行AJAX调用后在对话框中显示数据,java,jquery,ajax,spring,jsp,Java,Jquery,Ajax,Spring,Jsp,我不熟悉Spring和web技术 我有一个表,其中包含一个带有超链接的列。当我单击行的超链接时,我需要在对话框中显示该行的数据以及其他详细信息。我的控制器方法返回一个ModelAndView,其中包含我需要显示的数据和显示页面 问题: @RequestMapping(value = "businessBill.htm", method = RequestMethod.POST) @ResponseBody public String handleBusinessBillDetails(@Requ
ModelAndView
,其中包含我需要显示的数据和显示页面
问题:
@RequestMapping(value = "businessBill.htm", method = RequestMethod.POST)
@ResponseBody
public String handleBusinessBillDetails(@RequestParam("reference") String billReference, @RequestParam("invoiceDate") String billDate,
HttpServletRequest request, HttpServletResponse response) {
String json = null;
try {
//1. Create 'jackson' object mapper
ObjectMapper objectMapper = new ObjectMapper();
BusinessBillDTO businessBillDTO = new BusinessBillDTO();
businessBillDTO.setBillDate(sdf.parse(billDate));
businessBillDTO.setBillReference(billReference);
BusinessBillDTO billDto = accountStatementBO.getBusinessBillDetails(businessBillDTO);
//2. Convert your 'bean' or 'dto' as 'json' string
json = objectMapper.writeValueAsString(billDto);
} catch (Exception ex) {
LOGGER.error(ex);
}
return json;
}
<script type="text/javascript">
function showDialog(ref, date) {
$ajax({
type: "POST",
url: "/example/show.htm",
data: {
ref: ref,
date: date
}
success: function(data) {
},
error: function(data) {
}
});
}
</script>
你的代码是错误的,你在搞乱事情,如果你想使用
jQuery
和ajax
调用,那么不要在Spring
控制器中使用ModelAndView
。使用Java
中的Jackson
库,将以下内容和return
您的bean
或dto
用作json
:
将此jar
包含在lib
项目文件夹中:
@RequestMapping(value = "businessBill.htm", method = RequestMethod.POST)
@ResponseBody
public String handleBusinessBillDetails(@RequestParam("reference") String billReference, @RequestParam("invoiceDate") String billDate,
HttpServletRequest request, HttpServletResponse response) {
String json = null;
try {
//1. Create 'jackson' object mapper
ObjectMapper objectMapper = new ObjectMapper();
BusinessBillDTO businessBillDTO = new BusinessBillDTO();
businessBillDTO.setBillDate(sdf.parse(billDate));
businessBillDTO.setBillReference(billReference);
BusinessBillDTO billDto = accountStatementBO.getBusinessBillDetails(businessBillDTO);
//2. Convert your 'bean' or 'dto' as 'json' string
json = objectMapper.writeValueAsString(billDto);
} catch (Exception ex) {
LOGGER.error(ex);
}
return json;
}
Java代码:
@RequestMapping(value = "businessBill.htm", method = RequestMethod.POST)
@ResponseBody
public String handleBusinessBillDetails(@RequestParam("reference") String billReference, @RequestParam("invoiceDate") String billDate,
HttpServletRequest request, HttpServletResponse response) {
String json = null;
try {
//1. Create 'jackson' object mapper
ObjectMapper objectMapper = new ObjectMapper();
BusinessBillDTO businessBillDTO = new BusinessBillDTO();
businessBillDTO.setBillDate(sdf.parse(billDate));
businessBillDTO.setBillReference(billReference);
BusinessBillDTO billDto = accountStatementBO.getBusinessBillDetails(businessBillDTO);
//2. Convert your 'bean' or 'dto' as 'json' string
json = objectMapper.writeValueAsString(billDto);
} catch (Exception ex) {
LOGGER.error(ex);
}
return json;
}
然后,在Table.jsp
中,将Dialog.jsp
中使用的div
作为hidden
,这将是您未来的模式
对话框(请注意,span
标记中也有一些更改):
最后,如果一切都正确,您将在同一页面(
Table.jsp
)看到带有数据的modal
对话框,所有这些都是通过ajax
调用生成的,以避免像(Table.jsp
to=>dialog.jsp
)这样的页面重定向。这是什么类型的对话框,您使用的是某种modal对话框吗?因为它看起来像一个html
页面,如果您需要使用ajax
,那么最好使用模式对话框而不是页面。好的,让我试试使用模型对话框。@OscarJara我已经更新了代码。请告诉我如何将数据传递到对话框,并在从控制器返回后显示对话框。它不像你做的那么简单,请检查我的答案。+1。你能告诉我函数(data){}中的“data”是什么吗。因为没有什么比数据定义更好的了。抱歉,我不熟悉这些技术。@Chedata
只是用于表示Spring
控制器的回调中包含的内容的参数。不需要声明它
,它起作用了。我还有一个问题。那么我应该在什么时候使用model和view
。在这种情况下,我不需要重定向,因为我不需要重定向。当我们需要动态重定向到不同的页面时,是否使用了ModelAndView
?如果不使用jQuery
(例如,从一页到另一页传递参数,或者从控制器获取数据导航到特定页面的用户),请使用ModelAndView
。但是,如果您想要一些动态的东西,或者只是将一些数据带到客户端,请按照我在这里公开的方式操作。
function getBusinessBill(billReference, billInvoiceDate) {
$.post("/AccountStatement/businessBill.htm", {
reference: billReference,
invoiceDate: billInvoiceDate
}, function (data) {
/* You can implement more validations for 'data', in my case I just used these 'if' conditionals but can vary. */
if(data != null) { //returned 'data' is not 'null'
/* parse 'data' as 'json' object
* will be good to console.log(data) and take a look. */
var obj = $.parseJSON(data);
if(obj != {}) { //check if 'data' is not an empty 'json' object once transformed
//set the 'data' in the dialog
$('#dlg-account-number').text(obj.accountNumber);
$('#dlg-bill-date').text(obj.billDate);
/* open modal dialog, you can simulate it this way (for this case)
* but the correct way is to use 'jquery-ui' dialog or any plugin you prefer.
* At this point you will see the hidden 'div' in a visible way with your 'data'.
*/
$('#BusinessBill').fadeIn();
} else {
//show 'generic' message
alert('No results found.');
}
} else {
//show 'generic' message
alert('An error occurred, try again.');
}
});
}