資料庫的html內容顯示到頁面
阿新 • • 發佈:2019-01-29
額外篇:儲存html內容到資料庫,點選開啟連結
一、js傳送讀取html內容請求
var url = "...", data = {id:".."}; $.ajax({ url: url, type : "post", data : data, dataType: "json", success: function(data, textStatus, jqXHR){ //返回html內容結果,並顯示到當前頁面 var htmlContent = data; window.document.body.innerHTML = ""; //document.close(); document.write(htmlContent); }, error : function(jqXHR, textStatus, errorThrown){ alert(jqXHR + "," + textStatus + "," + errorThrown); } });
二、讀取資料庫的html內容
//本例使用ORACLE資料庫
String sql = "..."; List<Map<String, Object>> lists = jdbcTemplate().query(sql, new RowMapper<Map<String,Object>>(){ @Override public Map<String,Object> mapRow(ResultSet rs, int rowNum) throws SQLException { Map<String,Object> row = new HashMap<String,Object>(); row.put("CONTENT", lobHandler.getBlobAsBinaryStream(rs, "CONTENT"));//欄位CONTENT是BLOB型別 return row; }}); String htmlContent = null; InputStream inputStream = null; try{ inputStream = (InputStream) files.get(0).get("CONTENT"); htmlContent = IOUtils.toString(inputStream);//IOUtils是commons-io.jar的一個類 }finally{ if(inputStream != null) inputStream.close(); } return htmlContent;