freemarker加载模板目录的方法

Freemarker提供了3种加载模板目录的方法。 它使用Configuration类加载模板。

三种方法分别是:

public void setClassForTemplateLoading(Class clazz, String pathPrefix);

public void setDirectoryForTemplateLoading(File dir) throws IOException;

public void setServletContextForTemplateLoading(Object servletContext, String path);

第一种:基于类路径,HttpWeb包下的framemaker.ftl文件
  configuration.setClassForTemplateLoading(this.getClass(), "/HttpWeb");

configuration.getTemplate("framemaker.ftl"); //framemaker.ftl为要装载的模板 
第二种:基于文件系统

configuration.setDirectoryForTemplateLoading(new File("/template"))

configuration.getTemplate("framemaker.ftl"); //framemaker.ftl为要装载的模板

第三种:基于Servlet Context,指的是基于WebRoot下的template下的framemaker.ftl文件

HttpServletRequest request = ServletActionContext.getRequest();

configuration.setServletContextForTemplateLoading(request.getSession().getServletContext(), "/template");

configuration.getTemplate("framemaker.ftl"); //framemaker.ftl为要装载的模板

特别注意:tomcat启动和jar包启动加载的位置不同!

Configuration configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		/** 加载模板 **/
	    //这个方法在eclipse跑是OK 的 打jar包部署获取不到模版
    	/*File file = ResourceUtils.getFile("classpath:templates");
		configuration.setDirectoryForTemplateLoading(file);*/
		
		//	  这个方法在eclipse跑和打jar包部署都可以获取到模版
		configuration.setClassForTemplateLoading(this.getClass(), "/templates");
		Template template = configuration.getTemplate("研判报告上报模板.ftl");
  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
Freemarker中,模板文件的扩展名通常为`.ftl`,而不是`.tpl`。你可以创建一个以`.ftl`为扩展名的文件,并使用Freemarker模板语法编写动态内容。 以下是一个简单的FreeMarker模板示例: ```freemarker <html> <head> <title>Welcome to FreeMarker!</title> </head> <body> <h1>Welcome ${user}!</h1> <p>Here are your orders:</p> <ul> <#list orders as order> <li>${order}</li> </#list> </ul> </body> </html> ``` 在上面的模板中,`${user}`和`${order}`是Freemarker的变量插值语法,它们会被实际的值替换。`<#list>`语句用于遍历`orders`列表并生成相应的HTML代码。 你可以将上述模板保存在一个以`.ftl`为扩展名的文件中,例如`welcome.ftl`。 在使用Freemarker时,你需要创建一个`Configuration`对象,并将其配置为加载模板文件的位置。然后,使用`Template`对象加载模板,并将数据模型传递给它。最后,使用`Template.process()`方法将数据模型与模板合并,生成最终的输出。 以下是一个简单的Java示例: ```java Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); cfg.setClassForTemplateLoading(this.getClass(), "/templates"); Template template = cfg.getTemplate("welcome.ftl"); Map<String, Object> data = new HashMap<>(); data.put("user", "John"); data.put("orders", Arrays.asList("Order 1", "Order 2", "Order 3")); Writer out = new StringWriter(); template.process(data, out); String output = out.toString(); ``` 在上面的示例中,我们将模板文件放在`/templates`目录下,并使用`cfg.setClassForTemplateLoading()`方法设置模板加载的位置。然后,我们创建一个包含用户和订单数据的`Map`对象,并将其传递给`template.process()`方法。最后,我们将生成的输出保存在一个字符串中。 希望这个示例能帮助你理解Freemarker模板的基本用法。请记住,Freemarker有更多的语法和功能,你可以参考官方文档以了解更多信息。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java大表哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值