---
title: "html页面转换为js，作为其他页面引入"
author: "danp1"
date: "2013-08-30T14:35:39Z"
tags: []
categories: ["编程语言"]
slug: "html-js"
draft: false
lastmod: "2013-08-30T14:35:39Z"
id: 1933989
url: "https://danp1.iteye.com/blog/1933989"
---

今天遇到一使用要求，将html页面以js的形式提供给其它网站使用。其效果类似于iframe.
public void achieveId(){
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
		String ledFile = "/gg/js/led.js";
		String eleFile = "/gg/js/ele.js";
		HashSet<TEsample> tEsamples = new HashSet<TEsample>();
		HashSet<TEsample> tEsamplesEle = new HashSet<TEsample>();
		try {
			Workbook book1 = Workbook.getWorkbook(new File(request.getSession().getServletContext().getRealPath("/exhibit/lighting/2013/huace.xls")));
			Sheet sheet1 = book1.getSheet(4);
			int rownum = sheet1.getRows();
			for(int r = random(rownum); r < rownum; r++){
				Cell cell1 = sheet1.getCell(1, r);
			    String catalog = cell1.getContents();
			    int ind = catalog.indexOf("=");
			    if (ind > 0) {
			    	String id = catalog.substring(ind+1);
			    	TEsample tEsample = alightOodiiDao.queryEsampleById(Long.parseLong(id));
			    	if(tEsample != null && tEsamples.size() < 5 && tEsamples.contains(tEsample) == false){
			    		tEsamples.add(tEsample);
			    	}
				}
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		StringBuffer bf = new StringBuffer();
		bf.append("<!DOCTYPE html PUBLIC \\\"-\\/\\/W3C\\/\\/DTD XHTML 1.0 Transitional\\/\\/EN\\\" \\\"http:\\/\\/www.w3.org\\/TR\\/xhtml1\\/DTD\\/xhtml1-transitional.dtd\\\">");
		bf.append("<html xmlns=\\\"http:\\/\\/www.w3.org\\/1999/xhtml\\\">");
		bf.append("<head>");
		bf.append("<meta http-equiv=\\\"Content-Type\\\" content=\\\"text/html; charset=UTF-8\\\" \\/>");
		bf.append("<title>数据<\\/title>");
		bf.append("<link href=\\\"http://www.oodii.com/v3/css/led.css\\\" rel=\\\"stylesheet\\\" type=\\\"text/css\\\" \\/>");
		bf.append("<\\/head>");
		bf.append("<body>");
		bf.append("<div class=\\\"product_list\\\">");
		bf.append("<div class=\\\"right_ad\\\"><a href=\\\"http://www.oodii.com/exhibit/lighting/2013/\\\" target=\\\"_blank\\\"><img src=\\\"http://www.oodii.com/v3/img/ledth_03.jpg\\\" width=\\\"152\\\" height=\\\"218\\\" \\/><\\/a><\\/div>");
		if (tEsamples != null && tEsamples.size() > 0) {
			Iterator it = tEsamples.iterator();
			while (it.hasNext()) {
				TEsample tEsample = (TEsample) it.next();
				bf.append("<li>");
				String picM = tEsample.getCoverPic();
				if(picM.indexOf("_s") > 0){
					picM = picM.replace("_s", "_m");
				}
				bf.append("<div class=\\\"img\\\"><a href=\\\"http://ecatalog.oodii.com/").append(tEsample.getId()).append(".html\\\" target=\\\"_blank\\\"><img src=\\\"http://image.oodii.com/").append(picM).append("\\\" width=\\\"120\\\" height=\\\"144\\\" \\/><\\/a><\\/div>");
				bf.append("<div class=\\\"title\\\"><a title=\\\"").append(tEsample.getEsampleName()).append("\\\" href=\\\"http://www.oodii.com/catalog.action?id=").append(tEsample.getId())
				.append("\\\" target=\\\"_blank\\\">").append(tEsample.getEsampleName()).append("<\\/a><\\/div>");
				bf.append("<\\/li>");
			}
		}
bf.append("<\\/ul><\\/div><\\/body><\\/html>");
String wr = "document.writeln(\""+bf.toString()+"\");"; 
OutputStreamWriter osw = null;
		try {
			osw = new OutputStreamWriter(new FileOutputStream(request.getSession().getServletContext().getRealPath(ledFile), false),"UTF-8");
		    osw.write(wr, 0, wr.length());
		}catch(Exception ex){
			 ex.printStackTrace();
		}finally{
			try {
				osw.flush();
				osw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}