1. 概述
1.1 問題描述
給 iframe 設置 src 嵌入某個報表時,往往會給報表傳遞初始的參數值,例如:
<iframe id="reportFrame" width="900" height="400" src="/webroot/decision/view/report?viewlet=/report.cpt&參數1=參數值&參數2=參數值..."></iframe>
若參數名稱、參數值甚至報表名稱中包含中文或者特殊字符時,如果不進行編碼轉換,可能會出現一系列問題,如找不到模板文件等,如下。
那麽如何實現帶中文的報表正确傳參呢?
1.2 實現思路
在給報表服務器發送請求之前,使用 JavaScript 先對 URL 編碼,然後再向服務器提交。避免了不同的操作系統、不同的浏覽器、不同的網頁字符集,導緻完全不同的編碼結果。因爲JavaScript 的輸出總是一緻的,所以就保證了服務器得到的數據是格式統一的。
因此需要對 URL 中的中文,包含模板名、參數名字和參數值,進行 encodeURIComponent 或者 encodeURI 編碼。詳細點擊 編碼轉換。
2. 示例
2.1 新增parameter_ch.html文件
完整代碼如下:
<html>
<head>
<title>FineReport Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="javascript">
function autoLoad(){
var addr = "/webroot/decision/view/report?viewlet=GettingStarted.cpt&"+encodeURIComponent("地區")+"="+encodeURIComponent("華東");
document.getElementById("reportFrame").src = addr;
}
window.onload = autoLoad;
</script>
</head>
<body>
<iframe id="reportFrame" width="900" height="400" ></iframe>
</body>
</html>
2.2 存放parameter_ch.html文件
将新增的parameter_ch.html文件放入路徑:D:\soft\FineReport_10.0\webapps\webroot\help\page_demo
2.3 預覽效果
已完成示例請參照%FR_HOME%/webapps/webroot/help/page_demo/parameter_ch.html
啓動設計器,輸入http://localhost:8075/webroot/help/page_demo/parameter_ch.html,效果如下圖:
注:不支持【UTF-8 無 BOM】的編碼格式。