1. 程式人生 > >springboot專案練習六 去除頁面上多餘的html程式碼

springboot專案練習六 去除頁面上多餘的html程式碼

  • 去除頁面多餘的元素,使用jsoup完成HTML頁面div的選擇拼接自己的html模板完成html的修改
  • 使用ligerui彈出視窗的方式修改檢視新聞的方式
        <!-- jsoup -->
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.8.3</version>
		</dependency>

修改生成靜態檔案的方法 gcreateHtmlFile

@Value("${html.rootDir}")
	private String htmlDir;

	public void gcreateHtmlFile(String id, String url) {
		// 通過配置檔案獲取存放靜態檔案的資料夾
		BufferedReader br = null;
		BufferedWriter out = null;
		try {
			String path = htmlDir;
			File rootDir = new File(path);
			if (!rootDir.isDirectory()) {
				rootDir.mkdirs();
			}
			URL newUrl = new URL(url);
			URLConnection openConnection = newUrl.openConnection();
			openConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//設定請求型別
			openConnection.setRequestProperty("Charset", "utf-8"); //字元
			openConnection.setDoInput(true);        //設定輸入流採用位元組流
			openConnection.setDoOutput(true);        //設定輸出流採用位元組流
			InputStream in = openConnection.getInputStream();
			// 接收
			String line;
			br = new BufferedReader(new InputStreamReader(in,"gbk"));
			StringBuffer strb = new StringBuffer();
			while ((line = br.readLine()) != null) {
				strb.append(line);
			}
			String html = strb.toString();
			Document parse = Jsoup.parse(html);
			parse.charset();
			Element element = parse.getElementById("endText");//獲取新聞內容主體div
			//建立檔案將字串寫入檔案
			File dig =new File(path + "/" + id + ".html");
			
			if(!dig.isFile()){ // 如果檔案不存在則建立檔案
				dig.createNewFile();
			}
			dig.setWritable(true);
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dig),"utf-8"));
			out.write("<!DOCTYPE html>"
			+"<head>"+"<meta charset='UTF-8'>"+
			"</head>"+
			"<body>");
			out.write(element.toString());
			out.write("</body></html>");
			out.flush();
//			FileUtils.copyInputStreamToFile(in, new File(path + "/" + id + ".html"));

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(br!=null){try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}}
			if(out!=null){try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}}
			
			
		}
	}
	

檢視ligerDialog.js檔案找到這個方法 $.ligerDefaults.Dialog中定義的引數

修改news.html中檢視方法

function showNews(id,url){
	var newUrl ="/news/getNews/"+id;	
$.post(newUrl,{url:url},function(data){
 		if(data.code==200){
 			$.ligerDialog.open(
 					{
 					url:data.data+".html", //後臺返回的檔案id
 					title:"新聞資訊_檢視",
 					lock:true,
 					width:1000,
 					height:540,
 					id:"news_VIEW",
 					buttons:[{name:'關閉'}]
 					});
//  			window.location.href=data.data+".html";
 		}
		
		
	});
}

效果圖如下:

頁面效果這樣就完成了。現在沒有使用到springmvc的檢視解析器。慢慢完成,資料抓取這一模組。和redis的key值管理