FreeMarker demo

写道


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.ext.beans.ResourceBundleModel;
import freemarker.ext.beans.BeansWrapper;

import java.io.StringWriter;
import java.util.Locale;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.Map;

/**
* @author jm
*/
public class FreeMarkerTest {

public static void main(String[] args) {
FreeMarkerTest test = new FreeMarkerTest();
test.sayHello("hermit", Locale.FRENCH);
test.sayHello("hermit", Locale.ENGLISH);
}

public void sayHello(String name, Locale locale) {
Configuration freemarkerCfg = new Configuration();
freemarkerCfg.setClassForTemplateLoading(this.getClass(), "/");

freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");
Template template;
Locale.setDefault(Locale.ENGLISH);
try {
template = freemarkerCfg.getTemplate("hello.ftl");
template.setEncoding("UTF-8");
Map<String,Object> root = new HashMap<String,Object>();
root.put("user", name);
ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("message", locale);
ResourceBundleModel rsbm = new ResourceBundleModel(RESOURCE_BUNDLE, new BeansWrapper());
root.put("bundle", rsbm);
StringWriter writer = new StringWriter();
template.process(root, writer);
System.out.println(writer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}

}
 
以下是一个简单的 Freemarker Java Demo,用于生成 HTML 文件: 首先,需要在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> ``` 然后,创建一个名为 `FreemarkerDemo.java` 的 Java 类,并添加以下代码: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class FreemarkerDemo { public static void main(String[] args) throws IOException, TemplateException { // 创建 Configuration 对象 Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); // 设置模板文件所在的目录 File templatesDir = new File("src/main/resources/templates"); configuration.setDirectoryForTemplateLoading(templatesDir); // 获取模板文件 Template template = configuration.getTemplate("index.ftl"); // 创建数据模型 Map<String, Object> dataModel = new HashMap<>(); dataModel.put("title", "Freemarker Demo"); dataModel.put("message", "Hello Freemarker!"); // 将模板文件和数据模型合并生成 HTML 文件 FileWriter writer = new FileWriter("index.html"); template.process(dataModel, writer); writer.close(); System.out.println("HTML 文件生成成功!"); } } ``` 在上面的代码中,我们首先创建了一个 `Configuration` 对象并设置了模板文件所在的目录。然后,我们获取了名为 `index.ftl` 的模板文件,并创建了一个数据模型,其中包含了 `title` 和 `message` 两个变量的值。最后,我们将模板文件和数据模型合并生成了一个名为 `index.html` 的 HTML 文件。 在运行程序之后,会在项目根目录下生成一个名为 `index.html` 的 HTML 文件,其内容如下: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Freemarker Demo</title> </head> <body> <h1>Hello Freemarker!</h1> </body> </html> ``` 这就是一个简单的 Freemarker Java Demo,用于生成 HTML 文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值