简单快速的用Java动态生成jsp/html前端页面。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36481052/article/details/78998581
 首先给大家说说动态生成jsp、html的好处,大家都应该见过“淘宝”和“京东”,在每个节日会将他们菜单栏变成与节日相匹配的图片,和页面。这样就避免了项目修改完成之后再从新部署。
首先给大家看一下看一下效果图:
![test.html](https://img-blog.csdn.net/20180107225231617?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzY0ODEwNTI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

我是直接用StringBuilder直接生成的,很简单。首先是我生成赫塔米勒的工具类:

public static void CreatHtml(String filePath){
        //创建、初始化stringHtml对象       
        StringBuilder stringHtml = new StringBuilder();
        //初始化文件对象
        PrintStream printStream =null;
        try{
            //打开文件
             printStream = new PrintStream(new FileOutputStream(filePath));
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        //追加输入HTML文件内容
        stringHtml.append("<html><head>");
        stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
        stringHtml.append("<title>测试报告文档</title>");
        stringHtml.append("</head>");
        stringHtml.append("<body>");
        stringHtml.append("<h1>简单快速用Java动态生成jsp/html页面</h1>");
        stringHtml.append("</body></html>");
        try{
            //将HTML文件内容写入文件中
            printStream.println(stringHtml.toString());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
检测一下工具类是否能用:
public static void main(String[] arg0){
        //文件储存路径
        String filePath = "D://test.html";
        //创建文件
        CreatHtml(filePath);
}

这就完事了,路过的大神请多多赐教,谢谢各位。

猜你喜欢

转载自blog.csdn.net/qq_36481052/article/details/78998581