页面静态化技术

  1. eclipse点击Window,选择Reference,弹出如下框
  2. 找到General --> Editors --> File Associations

如下如果有*.ftl文件,就不用点击上面的Add。如果没有,就点击Add新建

  1. 然后点击此页面下面的Add按钮:

 

  1. 然后选择General--Content Types 
  2.  编程步骤,这里我们写一个工具类方便我们使用

  3. package cn.jiedada.util;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.Writer;
    import java.util.UUID;
    
    import freemarker.template.Configuration;
    import freemarker.template.Template;
    
    public class FreeMakerUtil {
    
        /**
         * @param templatePath--ftl文件父目录路径
         * @param templateName--ftl文件名称
         * @param object --传入的数据
         * @param suffix    --文件后缀
         * @return
         */
        public static String createFile(String templatePath,String templateName,Object data,String suffix) {
            //创建一个配置对象
            Configuration conf = new Configuration(Configuration.VERSION_2_3_28);
            Writer pw = null;
            String htmluri=null;
            try {
                //创建父目录文件
                File file = new File(templatePath);
                //设置默认template文件路径及字符集
                conf.setDirectoryForTemplateLoading(file);
                conf.setDefaultEncoding("UTF-8");
                //创建template
                Template template = conf.getTemplate(templateName);
                //设置新的名字
                String uuid=UUID.randomUUID().toString().replace("-", "");
                htmluri = uuid+suffix;
                //设置输出路径
                pw =new PrintWriter(new File(file,htmluri));
                template.process(data, pw );
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if(pw!=null){
                    try {
                        pw.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            return htmluri;
        }
    }
    View Code

     传入的data只能为map或者实体类

  4. map和实体类的遍历
  5. 实体类直接使用字段名
  6. 多个实体的list对象添加到map中的遍历
  7. <html>
    
        <head>
        <meta charset="utf-8">
        </head>
        <body>
                   //传入的map中的数据as为我们的单条数据
            <#list list as l>
                城市:${l.cname }
                城市级别
                    <#if l.id lt 10>
                    一线城市
                    <#elseif l.id lt 21>
                    二线城市
                    <#else>
                    三线城市
                    </#if>
            </#list >
        </body>
    </html>        

     在项目中我们需要做的事情,因为我们的删除和更新都存入了html的地址所以我们需要在修改和删除的时候同时删除数据

  8. 这里为修改
  9. @RequestMapping("/modify")
        public String modify(Jobs jobs,HttpServletRequest req){
            //先删除url在修改
            String realPath = req.getServletContext().getRealPath("/freemakser");
            File file = new File(realPath,jobs.getHtmlurl());
            if(file.exists()){
                file.delete();
            }
            service.modify(jobs);
            return "forward:page";
        }

     这里为删除

  10. @RequestMapping("/del")
        public String delete(Integer id,String htmlurl,HttpServletRequest req){
            service.del(id);
            String realPath = req.getServletContext().getRealPath("/freemakser");
            File file = new File(realPath,htmlurl);
            if(file.exists()){
                file.delete();
            }
            return "forward:page";
        }
    View Code

     service中的修改为

  11. @Override
        public void modify(Jobs jobs) {
            String templatePath = "F:\\JAVAEE\\2019_09_11_cms4\\src\\main\\webapp\\freemakser";
            String templateName ="join_us_details.ftl";
            String htmlurl = FreeMakerUtil.createFile(templatePath, templateName, jobs, ".html");
            jobs.setHtmlurl(htmlurl);
            dao.modify(jobs);
        }
    
        @Override
        public void add(Jobs jobs) {
            String templatePath = "F:\\JAVAEE\\2019_09_11_cms4\\src\\main\\webapp\\freemakser";
            String templateName ="join_us_details.ftl";
            String htmlurl = FreeMakerUtil.createFile(templatePath, templateName, jobs, ".html");
            jobs.setHtmlurl(htmlurl);
            dao.add(jobs);
        }
    View Code

猜你喜欢

转载自www.cnblogs.com/xiaoruirui/p/11516998.html