freemarker加载模板文件的三种方法

freemarker加载模板文件的三种方法

加载类路径

  1. Configuration cfg= new Configuration();          
  2. freemarkerCfg.setClassForTemplateLoading(this.getClass(), "/");//类路径
  3. Template template = cfg.getTemplate(x.ftl);        

加载绝对路径

  1. cfg.setDirectoryForTemplateLoading(new File(System .getProperty("user.dir") + "\\template"));//绝对路径 
  2. Template template = cfg.getTemplate(x.ftl); 
加载WEB路径
  1. cfg.setServletContextForTemplateLoading(getServletContext(),"/templates");//webroot路径
  2. Template template = cfg.getTemplate(x.ftl);         

你可能感兴趣的:(freemarker)