FreeMarker(二)模板加载

FreeMarker(二)模板加载

  Freemarker使用freemarker.template.Configuration类进行模板加载,有3种设置模板存放路径的方法。分别是:

  (1)类路径

    public void setClassForTemplateLoading(Class resourceLoaderClass, String basePackagePath) {...}  

  (2)文件路径

    public void setClassLoaderForTemplateLoading(ClassLoader classLoader, String basePackagePath) {...}

  (3)Servlet上下文

    public void setServletContextForTemplateLoading(Object servletContext, String path) {...}

  

  在此之前可以点击此处了解绝对路径和相对路径的知识。

  Configuration config = new Configuration(new Version(2, 3, 22));//获取config对象,本人使用的版本是2.3.22

  一:类路径

  

  本例通过Test测试类获取testchild包下的template.html模板

  config.setClassForTemplateLoading(Test.class, "/testchild");//设置模板路径

  Template template = config.getTemplate("template.html");//获取模板

  二:文件路径

  获取项目template文件夹下的template.html模板

   config.setDirectoryForTemplateLoading(new File(System.getProperty("user.dir")+"/template"));//设置模板路径

  Template template = config.getTemplate("template.html");//获取模板

  三:Servlet上下文-基于WebRoot下的模板文件

  本例获取WebRoot下template文件夹下的template.html模板文件。

  configuration.setServletContextForTemplateLoading(request.getSession().getServletContext(), "/template");//设置模板路径

  Template template = config.getTemplate("template.html");//获取模板

   

 

转载于:https://www.cnblogs.com/maxudong/p/8274820.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值