没有配置文件,配置都由同事做完了。中文输出好像有点问题,但大体是实现了的
/**
*
* @函数名称:previewCityInfo controller层
* @创建日期:2019年4月19日
* @功能说明:
* @参数说明:
* @返回说明:void
*/
@RequestMapping(value = "/preview_cityInfo")
public void previewCityInfo(HttpServletRequest request, HttpServletResponse response) {
// 拿到session
Session session = SecurityUtils.getSubject().getSession();
CityInfoQ cityCodeQ = (CityInfoQ) session.getAttribute("cityCodeQ");
List<Order> orders = this.generateOrders(cityCodeQ);
List<Where> wheres = this.generateWheres(cityCodeQ);
Map<String,String> countryMap = new HashMap<String,String>();
Pager<CityInfo> pager = new Pager<>(0, 20);
try {
// 分页查询所有城市信息
pager = cityInfoService.getCityInfos(cityCodeQ.getNumPerPage(), cityCodeQ.getPageNum(), null, wheres, orders);
Map<String, Object> maps = new HashMap<String, Object>();
maps.put("cityCodeQ", cityCodeQ.getCityCodeQ());
maps.put("cityNameQ", cityCodeQ.getCityNameQ());
maps.put("countryCodeQ", cityCodeQ.getCountryCodeQ());
cityInfoService.journalPreview(response, pager.getResults(), maps);
} catch (Exception e) {
log.error("cityInfo log pager query:", e);
}
}
/**serviceImpl层
* 报表
*/
@Override
public void journalPreview(HttpServletResponse response, List<CityInfo> results, Map<String, Object> maps) throws Exception {
// TODO Auto-generated method stub
File reportFile = null;
reportFile = ResourceUtils.getFile("classpath:reports/firstReport.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile);
JasperPrint jasperPrint = null;
try {
jasperPrint = JasperFillManager.fillReport(jasperReport, maps, new JRBeanCollectionDataSource(results));
} catch (JRException e) {
e.printStackTrace();
}
if (null != jasperPrint) {
FileBufferedOutputStream fbos = new FileBufferedOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
try {
exporter.exportReport();
fbos.close();
if (fbos.size() > 0) {
response.setContentType("application/pdf");
response.setContentLength(fbos.size());
ServletOutputStream ouputStream = response.getOutputStream();
try {
fbos.writeData(ouputStream);
fbos.dispose();
ouputStream.flush();
} finally {
if (null != ouputStream) {
ouputStream.close();
}
}
}
} catch (JRException e1) {
e1.printStackTrace();
} finally {
if (null != fbos) {
fbos.close();
fbos.dispose();
}
}
}
}
/*html 页面*/
<td><a class="edit" id = "printId" "printJournalPreview()" title="打印日报"><span>打印</span></a></td>
<div class="loading" style="display: none;font-weight:800;color:#fff;font-size: 16px;position: absolute;top: 50%;left: 40%;z-index: 9999999999;transform: translate(-50%,-50%);">加载中...</div>
<div id="bground_print" style="display: none;position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; z-index: 10000; background: rgb(119, 119, 119); opacity: 0.6;"></div>
<div id="content_print" style="display: none;z-index:999999999;position: absolute;top: 57%;left: 40%;transform: translate(-50%,-50%);"></div>
<script type="text/javascript">
function printJournalPreview(){
var tr="";
tr=document.getElementsByName('printreport');
if (tr.length <= 0) {
alertMsg.error("打印数据不能为空!");
}else{
$(".loading").show();
// EV_modeAlert();//弹出遮罩层
var iframe = '<div style="position:fixed;top: 52px;right: 20px;color:#fff;z-index:1;"> <button style="with:80px;height:33px;font-size: 20px;" type="button" onclick ="closeIframe()">关闭</button></div><iframe target="target1" src="/city_info/preview_cityInfo?flag=true" target="_blank" width="900" height="500" scrolling="no" border="0" ></iframe>';
//延时加载
setTimeout(function(){
$(".loading").hide();
$("#content_print").append(iframe);
}, 1000);
$("#bground_print").show();//显示遮盖层
$("#content_print").show();//显示隐藏的区域
}
}
//关闭打印的弹出区域
function closeIframe(){
$("#bground_print").hide();
$("#content_print").hide();
$("#content_print").empty();
}
</script>