项目中要将盘点数据生成pdf,我们直接使用的jsp去生成pdf。为了避免controller代码过于繁琐,所以我们将数据提交到jsp页面进行处理后直接生成pdf。
但是在传递参数的过程我刚开始使用的是ajax提交数据并跳转到jsp页面最后生成pdf,但是测试多遍后发现在springmvc中,使用ajax提交数据的话,返回的数据不能是html,否则无法执行。于是,有了如下的做法:function createCode(){
var rows = $('#ctm').datagrid('getSelections');
alert(JSON.stringify(rows));
if(rows!=""){
StandardPost('../../dragon/data/model',{rows:JSON.stringify(rows)});
}else{
$.messager.alert('操作提示', '请先选中资产!', 'error');
}
}
function StandardPost (url,args)
{
var form = $("<form method='post'></form>");
form.attr({"action":url});
for (arg in args)
{
var input = $("<input type='hidden'>");
input.attr({"name":arg});
input.val(args[arg]);
form.append(input);
}
form.submit();
}
直接使用了post提交隐藏的数据然后跳转到jsp最后生成pdf。controller只负责接收数据并传递到jsp,生成pdf在jsp中处理。
注意生成pdf需要这样做:
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+file_name);
使用了非ajax提交。