Ama*_*ath 4 java ajax jquery spring jsp
我是Spring和Web技术的新手.
我有一个表,其中包含一个带超链接的列.当我单击行的超链接时,我需要在对话框中显示行数据以及其他详细信息.我的控制器方法返回一个ModelAndView包含我需要显示的数据和显示页面的方法.
问题:
如何显示对话框?和
如何将数据传递给对话框?
Table.jsp
<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>
Run Code Online (Sandbox Code Playgroud)
制图
@RequestMapping(value = "show.htm", method=RequestMethod.POST)
public ModelAndView show(@RequestParam("ref") String ref, @RequestParam("date") String date,
HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView();
try {
SampleDTO SampleDTO = new SampleDTO();
sampleDTO.setDate(sdf.parse(date));
sampleDTO.setRef(ref);
SampleDTO billDto = // server call modelAndView.addObject("showBill", sampleDto);
modelAndView.setViewName("Dialog");
}
return modelAndView;
}
Run Code Online (Sandbox Code Playgroud)
你的代码是错误的,你搞乱的东西,如果你想使用jQuery和ajax调用,那么不要ModelAndView在你的Spring控制器中使用.而不是那样,使用以下和return您bean或dto作为json使用Jackson库Java:
将其包含jar在lib项目文件夹中:
http://www.java2s.com/Code/JarDownload/jackson/jackson-all-1.9.9.jar.zip
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;
}
Run Code Online (Sandbox Code Playgroud)
然后,Table.jsp把div用于Dialog.jsp作为hidden,这将是你modal未来的对话框(注意,是在一些变化span标签也):
<div id="BusinessBill" style="display:none;">
<h2>Bill Details</h2>
<em>Business Ltd</em>
<div class="row">
<span class="spanAsLabel">Account number</span>
<span id="dlg-account-number" class="spanAsLabel"></span>
</div>
<div class="row">
<span class="spanAsLabel">Bill date</span>
<span id="dlg-bill-date" class="spanAsLabel"></span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
现在修复你的getBusinessBill(..)方法,如下所示:
你也可以使用$.ajax也许处理更多的状态,比如onerror和其他状态,但这种方式更简单(至少对我来说,你只需要评估返回的data是否是null,并且让我们知道user- 如果null - 发生了什么事情server,可能会显示alert一般信息) - 请阅读评论.
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.');
}
});
}
Run Code Online (Sandbox Code Playgroud)
最后,如果一切正确,您将在同一页面上看到(Table.jsp)modal与您的对话data,所有这些都是通过ajax调用来避免重定向页面,如(Table.jspto => Dialog.jsp).
| 归档时间: |
|
| 查看次数: |
12767 次 |
| 最近记录: |