1、使用java+freeMarker实现。
2、使用java的IO写HTML文档。
3、使用URLRewriter将*.jsp伪装成*.html来访问。
4、将html的内容存入数据库中,在运行时读取出来在服务端输出成html。最后把请求转至html
第一种最简单,不知道你用过没有。简单的说,就是你想生成一个动态的html,你只需要html页面的静态部分写死,作为模板,动态的数据部分,用java的Map封装起来,然后交给freeMarker,它会帮你写入html文档中。最后给你生成你想要的html文档。这个html文档具有你的静态结构,加上动态数据。我给你个示例。
import java.io.ByteArrayOutputStreamimport java.io.OutputStreamWriterimport java.io.Writerimport java.util.Localeimport java.util.Map
import freemarker.template.Configurationimport freemarker.template.DefaultObjectWrapperimport freemarker.template.Template
/** * 使用freeMarker模板生成报表的动态数据代码 * @author wzj * 2013-09-18 */public class ReportFreeMarker { private static Configuration cfg = new Configuration()private static final String FTL_FILE = "chart_templet_" static { try { cfg.setDefaultEncoding("utf-8") cfg.setClassForTemplateLoading(ReportFreeMarker.class, "") cfg.setObjectWrapper(new DefaultObjectWrapper()) } catch (Exception e) { e.printStackTrace() } } // public static void main(String[] args) throws Exception {// new ReportFreeMarker().execute("chart_templet.ftl")//}
/** * 根据type来决定使用哪个模板文件,生成对应的柱图,饼图(1-柱图,2-饼图) * @param parameterMap 参数集 * @param type 图表类别 * @return * @throws Exception */ public String buildChartCode(Map parameterMap, int type) throws Exception { Template t = cfg.getTemplate(FTL_FILE + type + ".ftl") t.setEncoding("utf-8") String result = null Writer out = null ByteArrayOutputStream bos = new ByteArrayOutputStream() try { out = new OutputStreamWriter(bos) t.process(parameterMap, out) out.flush() result = new String(bos.toByteArray()) } catch (Exception e) { e.printStackTrace() throw e } finally { if (out != null) {out.close() } if ( bos != null ) bos.close() bos = null } return result
// File tmp = new File("./tmp.xml")//写成tmp.xml测试// if ( tmp.exists() ) tmp.delete()// Writer out = null// try {// out = new OutputStreamWriter(new FileOutputStream(tmp), "utf-8")// t.process(root, out)// out.flush()// } catch (Exception e) {// e.printStackTrace()// throw e// } finally {// if (out != null) {//out.close()// }// } }}
//下面是调用
ReportFreeMarker marker = new ReportFreeMarker() Map root = new HashMap()
root.put(CHART_XLIST, xlist) root.put(CHART_YLIST, ylist) String content = null try { content = marker.buildChartCode(root, chartType)//根据模板生成图表xml代码 } catch (Exception e) { e.printStackTrace() throw new ReportException(ERROR_FREEMARKER_FAILED + "(" + e.getMessage() + ")") }
这样,你最后可以得到一个包含动态数据的静态html页面的代码,保存成一个文件。
////在html中(模板)可以这样写
<html><body>
<#list ylist as item> <!--这就是动态的,要封装成map传入--> <table></table>
</#list>
</body>
</html>