framework freemarker / velocity / freemark / thymeleaf

s

https://www.cnblogs.com/beijie/p/16825518.html

什么是Thymeleaf?

Thymeleaf 官网是这么解释的:Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
译过来就是:Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎
模板引擎介绍
模板引擎?你可能第一次听说模板引擎,估计你会禁不住想问:什么是模板引擎?

模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的html文档。从字面上理解模板引擎,最重要的就是模板二字,这个意思就是做好一个模板后套入对应位置的数据,最终以html的格式展示出来,这就是模板引擎的作用。
对于模板引擎的理解,可以这样形象的做一个类比:开会! 相信你在上学初高中时候每次开会都要提前布置场地、拿小板凳、收拾场地。而你上了大学之后每次开会再也不去大操场了,每次开会都去学校的大会议室,桌子板凳音响主席台齐全,来个人即可,还可复用……。模板引擎的功能就类似我们的会议室开会一样开箱即用,将模板设计好之后直接填充数据即可而不需要重新设计整个页面。提高页面、代码的复用性。



不仅如此,在Java中模板引擎还有很多,模板引擎是动态网页发展进步的产物,在最初并且流传度最广的jsp它就是一个模板引擎。jsp是官方标准的模板,但是由于jsp的缺点比较多也挺严重的,所以很多人弃用jsp选用第三方的模板引擎,市面上开源的第三方的模板引擎也比较多,有Thymeleaf、FreeMaker、Velocity等模板引擎受众较广。

听完了模板引擎的介绍,相信你也很容易明白了模板引擎在web领域的主要作用:让网站实现界面和数据分离,这样大大提高了开发效率,让代码重用更加容易。

Thymeleaf介绍
上面知晓了模板引擎的概念和功能,你也知道Thymeleaf是众多模板引擎的一种,你一定会好奇想深入学习Thymeleaf的方方面面。从官方的介绍来看,Thymeleaf的目标很明确:

Thymeleaf的主要目标是为您的开发工作流程带来优雅自然的模板-HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而可以在开发团队中加强协作。
Thymeleaf拥有适用于Spring Framework的模块,与您喜欢的工具的大量集成以及插入您自己的功能的能力,对于现代HTML5 JVM Web开发而言,Thymeleaf是理想的选择——尽管它还有很多工作要做。
并且随着市场使用的验证Thymeleaf也达到的它的目标和大家对他的期望,在实际开发有着广泛的应用。Thymeleaf作为被Springboot官方推荐的模板引擎,一定有很多过人和不寻同之处:

动静分离: Thymeleaf选用html作为模板页,这是任何一款其他模板引擎做不到的!Thymeleaf使用html通过一些特定标签语法代表其含义,但并未破坏html结构,即使无网络、不通过后端渲染也能在浏览器成功打开,大大方便界面的测试和修改。
开箱即用: Thymeleaf提供标准和Spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改JSTL、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
Springboot官方大力推荐和支持,Springboot官方做了很多默认配置,开发者只需编写对应html即可,大大减轻了上手难度和配置复杂度。



此外,Thymeleaf在曾经还有一次大的版本升级,从Thymeleaf2.0—>Thymeleaf3.0。在Thymeleaf2.0时代,Thymeleaf基于xml实现,虽然它带来了许多出色强大的功能,但有时会降低性能效率,那个时候Thymeleaf的性能真的太差而被很多人所吐槽带来了很不好的印象。

但是Thymeleaf3.0对比Thymeleaf2.0有着翻天覆地的变化,几乎是全部重写了整个Thymeleaf引擎,在性能、效率上相比Thymeleaf2有了很大改善,能够满足更多项目的需求,且Thymeleaf3.0不再基于xml所以在html环境下有着更宽松的编程环境。

此外,Thymelaf3.0在方言、独立于Java Servlet API、重构核心API、片段表达等方面有着巨大提升和改善,具体可以参看Thymeleaf3十分钟参考指南。


学习Thymeleaf必知的知识点
Thymeleaf模板的运行离不开web的环境,所以你需要对相关知识学习理解才能更好的有助于你对Thymeleaf的学习和认知。

Springboot
相信你对Springboot都很熟悉,我们使用Thymeleaf大多情况都是基于Springboot平台的,并且Thymeleaf的发展推广也离不开Springboot官方得支持,且本文的实战部分也是基于Springboot平台。

而Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

简而言之,Springboot是当前web开发主流,且其简化了Spring的配置让开发者能够更容易上手Web项目的开发。且Thymeleaf能够快速整合入Springboot,使用方便快捷。

 

 

CMS 静态化参考

 

JAVA静态化,如何生成html问题

http://hi.csdn.net/yuezu1026/

http://topic.csdn.net/u/20080829/14/7ee4965f-f0d3-4c84-a2fa-10d44b735d63.html

 

http://hi.csdn.net/yuezu1026/

中国灯饰商贸网

www.lighting86.com.cn

中山市旅游网

http://www.86760.com/

 

 

public class Test {

    //config.getDriverName(),config.getUrl(),config.getUserName(),config.getPassword());
    public static void main(String[] args) {
        Config config = ConfigResolver.parseXML(null);
       
        String jspPage = config.getIndexFrom();//jsp文件地址
        String htmlPage = config.getIndexTo();//html文件名 
        if(jspPage!=null && htmlPage!=null ){
            createHtml(jspPage,htmlPage); //处理站点首页
        }
       
        jspPage = config.getGroupZhengquanFrom();//jsp文件地址
        htmlPage = config.getGroupZhengquanTo();//html文件名
        if(jspPage!=null && htmlPage!=null ){
            createHtml(jspPage,htmlPage); //处理证券圈子页面
        }
public static void createHtml(String jspPage,String htmlPage){
        System.out.println(jspPage+"----------------start");
        BufferedReader in = null;
        BufferedWriter out= null;
        try{
            in = new BufferedReader(new InputStreamReader(new FileInputStream(htmlPage)));
            out = getOut(htmlPage+"_old", true); //备分原文件
            copyFile(in,out); //备分原文件
        }catch(Exception e){
            e.printStackTrace();
        }       
       
        in = getIn(jspPage);
        out = getOut(htmlPage+"_back", true);
        generateHtml(in, out,jspPage);//生成静态页面
        close(in, out);
       
        try{
            in=new BufferedReader(new InputStreamReader(new FileInputStream(htmlPage+"_back")));
        }catch(Exception e){
            e.printStackTrace();
        }
        out = getOut(htmlPage, true);
        copyFile(in,out); //文件拷贝
        System.out.println(jspPage+"----------------end");
    }

    public final static void generateHtml(BufferedReader in, BufferedWriter out,String jspPage) {
        boolean flag=true;

        if (out == null)
            return;//不生成静态页面
        try {
            out.write(" <%@ page language=\"java\" %>\r\n");
            out.write(" <%@ page contentType=\"text/html; charset=GBK\"%>\r\n");
            String c;
            while ((c = in.readLine()) != null) {
//                if(c.trim().equals(" <!--portalheaderbegin-->")){           
//                    flag=false;
//                }
//                if(c.trim().equals(" <!--portalheaderend-->")){
//                    if(jspPage.indexOf("channel.jsp")!=-1){ //如果是频道页面
//                        c=" <jsp:include page='/include/portalheader.jsp?channelId="+jspPage.split("type_id=")[1]+"' />\r\n";
//                    }else if(jspPage.indexOf("zhengquan_group_act.jsp")!=-1){ //证券圈
//                        c=" <jsp:include page='/include/portalheader.jsp?groupId=43' />\r\n";
//                    }else if(jspPage.indexOf("group_general.jsp")!=-1){ //其他圈子
//                        c=" <jsp:include page='/include/portalheader.jsp?groupId="+jspPage.split("type_id=")[1]+"' />\r\n";
//                    }else if(jspPage.indexOf("group_index")!=-1){ //圈子首页
//                        c=" <jsp:include page='/include/portalheader.jsp?groupId=group' />\r\n";
//                    }else if(jspPage.indexOf("community_index")!=-1){ //社区首页
//                        c=" <jsp:include page='/include/portalheader.jsp?shequId=abc123' />\r\n";
//                    }else{
//                        c=" <jsp:include page='/include/portalheader.jsp' />\r\n";
//                    }
//                    flag=true;
//                }
               
                if(c.trim().equals(" <!--portalheaderbegin-->")){           
                    flag=false;
                }
               
                if(c.trim().equals(" <!--portalheaderend-->")){
                    if(jspPage.indexOf("channel.jsp")!=-1){ //如果是频道页面
                        //c=" <jsp:include page='/include/portalheader.jsp?channelId="+jspPage.split("type_id=")[1]+"' />\r\n";
                        c=" <jsp:include page='/permissionFilter.jsp' />\r\n";
                    }else if(jspPage.indexOf("zhengquan_group_act.jsp")!=-1){ //证券圈
                        c=" <jsp:include page='/include/portalheader.jsp?groupId=43' />\r\n";
                    }else if(jspPage.indexOf("group_general.jsp")!=-1){ //其他圈子
                        //c=" <jsp:include page='/include/portalheader.jsp?groupId="+jspPage.split("type_id=")[1]+"' />\r\n";
                        c=" <jsp:include page='/permissionFilter.jsp' />\r\n";
                    }else if(jspPage.indexOf("group_index")!=-1){ //圈子首页
                        c=" <jsp:include page='/include/portalheader.jsp?groupId=group' />\r\n";
                    }else if(jspPage.indexOf("community_index")!=-1){ //社区首页
                        c=" <jsp:include page='/include/portalheader.jsp?shequId=abc123' />\r\n";
                    }else if(jspPage.indexOf("portal/index_act.jsp")!=-1){ //首页
                        //c=" <jsp:include page='/include/portalheader.jsp?index=yes' />\r\n";
                        c=" <jsp:include page='/permissionFilter.jsp' />\r\n";
                    }else if(jspPage.indexOf("portal/blogershare_act.jsp")!=-1){ //分享页
                        c=" <jsp:include page='/permissionFilter.jsp' />\r\n";
                    }else{
                        c=" <jsp:include page='/include/portalheader.jsp' />\r\n";
                    }
                    flag=true;
                }
                if(flag&&c.length()>0){
                    out.write(c+"\r\n");
                }
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public final static void copyFile(BufferedReader from, BufferedWriter to) {
        if (to == null || from == null)
            return;//不生成静态页面
        try {
            int c;
                while ((c = from.read()) != -1) {
                    to.write((char)c);
            }
            to.flush();
            close(from, to);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public final static BufferedReader getIn(String jspPage) {

        BufferedReader in = null;
        URL url = null;
        URLConnection urlConn = null;
        try {
            url = new URL(jspPage);
            urlConn = url.openConnection();//建立http连接
            in = new BufferedReader(new InputStreamReader(urlConn
                    .getInputStream(), "GBK"));//设置Encoding,必须设置,否则显示中文会有问题
        } catch (IOException e) {
            e.printStackTrace();
           
        }finally{
            urlConn=null;
            url=null;
        }
       

        return in;
    }

    public final static BufferedWriter getOut(String htmlPage, boolean flag) {

        BufferedWriter out = null;

        try {
            File htmlFile = new File(htmlPage);
            if (flag) {
                htmlFile.createNewFile();
            } else {//如果flag为false则不覆盖文件
                if (htmlFile.exists()) {//如果文件已经存在则返回null
                    return null;
                } else {//建立新文件
                    htmlFile.createNewFile();
                }
            }
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(htmlFile), "GBK"));//设置Encoding
        } catch (IOException e) {
            e.printStackTrace();
        }

        return out;

    }//flag为真则覆盖原文件

    public final static void close(BufferedReader in, BufferedWriter out) {

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

    }
}

 

 

public class ConfigResolver {
    private static String xmlPath = "config/config.xml";

    /**
    * 获取配置文件对象信息
    *
    * @param fileName
    * @return
    * @throws Exception
    */
    public static Config parseXML(String fileName) {
        if (null != fileName) {
            xmlPath = fileName;
        }
        Config configFile = new Config();
        try {
            XMLConfiguration parser = new XMLConfiguration();
            parser.setFile(new File(xmlPath));
            parser.load();

            //首页原
            String indexFrom = (String) parser.getProperty("indexFrom");
            configFile.setIndexFrom(indexFrom);
            //首页目标
            String indexTo = (String) parser.getProperty("indexTo");
            configFile.setIndexTo(indexTo);

            //圈子页原
            String groupZhengquanFrom = (String) parser
                    .getProperty("groupZhengquanFrom");
            configFile.setGroupZhengquanFrom(groupZhengquanFrom);
            //圈子页目标
            String groupZhengquanTo = (String) parser
                    .getProperty("groupZhengquanTo");
            configFile.setGroupZhengquanTo(groupZhengquanTo);

            //频道页原
            String channelFrom = (String) parser.getProperty("channelFrom");
            configFile.setChannelFrom(channelFrom);
            //频道页目标
            String channelTo = (String) parser.getProperty("channelTo");
            configFile.setChannelTo(channelTo);

            String channelIds = (String) parser.getProperty("channelIds");
            configFile.setChannelIds(channelIds);

            //大杂烩原
            String dazahuiFrom = (String) parser.getProperty("dazahuiFrom");
            configFile.setDazahuiFrom(dazahuiFrom);
            //大杂烩目标
            String dazahuiTo = (String) parser.getProperty("dazahuiTo");
            configFile.setDazahuiTo(dazahuiTo);

            //别人的订阅,关注原
            String anthorTakenFrom = (String) parser
                    .getProperty("anthorTakenFrom");
            configFile.setAnthorTakenFrom(anthorTakenFrom);
            //别人的订阅,关注目标
            String anthorTakenTo = (String) parser.getProperty("anthorTakenTo");
            configFile.setAnthorTakenTo(anthorTakenTo);

            //房产圈子页原
            String fangChanFrom = (String) parser
                    .getProperty("groupFangchanFrom");
            configFile.setGroupFangchanFrom(fangChanFrom);
            //房产圈子页目标
            String fangChanTo = (String) parser.getProperty("groupFangchanTo");
            configFile.setGroupFangchanTo(fangChanTo);
            String groupIds = (String) parser.getProperty("groupIds");
            configFile.setGroupIds(groupIds);

            //首页用圈子文章list原
            String quanziArticleListFrom = (String) parser.getProperty("quanziArticleListFrom");
            configFile.setQuanziArticleListFrom(quanziArticleListFrom);
            //首页用圈子文章list目标
            String quanziArticleListTo = (String) parser.getProperty("quanziArticleListTo");
            configFile.setQuanziArticleListTo(quanziArticleListTo);
           
            String quanziIds = (String) parser.getProperty("quanziIds");
            configFile.setQuanziIds(quanziIds);
           
           
            //圈子首页原
            String groupIndexFrom = (String) parser.getProperty("groupIndexFrom");
            configFile.setGroupIndexFrom(groupIndexFrom);
            //圈子首页目标
            String groupIndexTo = (String) parser.getProperty("groupIndexTo");
            configFile.setGroupIndexTo(groupIndexTo);
           
            //社区首页原
            String shequIndexFrom = (String) parser.getProperty("shequIndexFrom");
            configFile.setShequIndexFrom(shequIndexFrom);
            //社区首页目标
            String shequIndexTo = (String) parser.getProperty("shequIndexTo");
            configFile.setShequIndexTo(shequIndexTo);
           
            //热点排名周源
            String articleRankWeekFrom = (String) parser.getProperty("articleRankWeekFrom");
            configFile.setArticleRankWeekFrom(articleRankWeekFrom);
            //热点排名周目标
            String articleRankWeekTo = (String) parser.getProperty("articleRankWeekTo");
            configFile.setArticleRankWeekTo(articleRankWeekTo);
           
            //热点排名月源
            String articleRankMonthFrom = (String) parser.getProperty("articleRankMonthFrom");
            configFile.setArticleRankMonthFrom(articleRankMonthFrom);
            //热点排名月目标
            String articleRankMonthTo = (String) parser.getProperty("articleRankMonthTo");
            configFile.setArticleRankMonthTo(articleRankMonthTo);
           
            //图片首页more源
            String photoIndexMoreFrom = (String) parser.getProperty("photoIndexMoreFrom");
            configFile.setPhotoIndexMoreFrom(photoIndexMoreFrom);
            //图片首页more目标
            String photoIndexMoreTo = (String) parser.getProperty("photoIndexMoreTo");
            configFile.setPhotoIndexMoreTo(photoIndexMoreTo);
           
            //博客分享源
            String blogerShareFrom = (String) parser.getProperty("blogerShareFrom");
            configFile.setBlogerShareFrom(blogerShareFrom);
            //博客分享目标
            String blogerShareTo = (String) parser.getProperty("blogerShareTo");
            configFile.setBlogerShareTo(blogerShareTo);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return configFile;
    }
}

 

 

 

end

posted @ 2009-03-26 21:17  siemens800  阅读(30)  评论(0编辑  收藏  举报