1、需要引入的自定义CSS和第三方库:
1.1 review.css
.fade {
opacity: 0;
-webkit-transition: opacity 0.15s linear;
-moz-transition: opacity 0.15s linear;
-o-transition: opacity 0.15s linear;
transition: opacity 0.15s linear;
}
.fade.in {
opacity: 1;
}
.collapse {
position: relative;
height: 0;
overflow: hidden;
-webkit-transition: height 0.35s ease;
-moz-transition: height 0.35s ease;
-o-transition: height 0.35s ease;
transition: height 0.35s ease;
}
.collapse.in {
height: auto;
}
.close {
float: right;
font-size: 20px;
font-weight: bold;
line-height: 20px;
color: #000000;
text-shadow: 0 1px 0 #ffffff;
opacity: 0.2;
filter: alpha(opacity=20);
}
.close:hover,
.close:focus {
color: #000000;
text-decoration: none;
cursor: pointer;
opacity: 0.4;
filter: alpha(opacity=40);
}
button.close {
padding: 0;
cursor: pointer;
background: transparent;
border: 0;
-webkit-appearance: none;
}
.modal-backdrop {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 1040;
background-color: #000000;
}
.modal-backdrop.fade {
opacity: 0;
}
.modal-backdrop,
.modal-backdrop.fade.in {
opacity: 0.8;
filter: alpha(opacity=80);
}
.modal {
position: fixed;
top: 10%;
left: 50%;
z-index: 1050;
width: 560px;
margin-left: -280px;
background-color: #ffffff;
border: 1px solid #999;
border: 1px solid rgba(0, 0, 0, 0.3);
*border: 1px solid #999;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
outline: none;
-webkit-box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
-webkit-background-clip: padding-box;
-moz-background-clip: padding-box;
background-clip: padding-box;
}
.modal.fade {
top: -25%;
-webkit-transition: opacity 0.3s linear, top 0.3s ease-out;
-moz-transition: opacity 0.3s linear, top 0.3s ease-out;
-o-transition: opacity 0.3s linear, top 0.3s ease-out;
transition: opacity 0.3s linear, top 0.3s ease-out;
}
.modal.fade.in {
top: 10%;
}
.modal-header {
padding: 9px 15px;
border-bottom: 1px solid #eee;
}
.modal-header .close {
margin-top: 2px;
}
.modal-header h3 {
margin: 0;
line-height: 30px;
}
.modal-body {
position: relative;
max-height: 400px;
padding: 15px;
overflow-y: auto;
}
.modal-form {
margin-bottom: 0;
}
img[data-toggle=modal]{
cursor:pointer;
}
.bs-example-modal-lg,.modal-dialog{
width: 800px;
}
1.2 jquery.min.js
1.3 bootstrap.min.js
2、前端页面代码以及发送的ajax请求处理
2.1 前端页面核心代码
2.2 自定义javascript发送ajax请求
function getPreview(filename) {
//console.log("filename==>" + filename);
$.ajax({
url : "/saas/previewPDF",
contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
data : {
fileName : filename,
},
success : function(data) {
$(".modal-title-x").text(filename);
$(".modal-body-x").html(data);
}
});
}
@RequestMapping("/previewPDF")
public String previewPDF(String fileName,Model model) {
model.addAttribute("fileName", fileName+".pdf");
return "layout/preview-pdf";
}
4、根目录下layout文件夹下新建preview-pdf.html,代码如下:
PDF预览
5、小结:
注意事项:
5.1、前台向后台传递文件名(中文)要注意传参时的字符编码问题;
5.2、文件的存放目录问题,将文件放置在static下的指定目录下,注意context-path、src问题;
5.3、为什么可以使用@requestMapping结合html+modal的形式实现PDF格式文件的预览?(以下是笔者个人小结,仅供参考)
使用@requestMapping接收到(ajax方式向后台发送的请求)前台的参数(文件名),并没有直接返回data作为ajax的success的回调用值,而是先在根目录下找到layout下的preview-pdf.html文件,而且参数model.addAttribute("fileName", fileName+".pdf")中的fileName是可以先传递到preview-pdf.html,这样通过iframe中的src获取项目中的文件,并将嵌入的文件以整个html的形式作为响应值(或者返回值)回调给ajax,success : function(data);再通过$(".modal-body-x").html(data)赋值给模态框。
6、展示页面效果