<html><head><title>${title}</title></head><body><h1>${message}</h1><ul><#list users as user><li>${user.name}(${user.age})</li></#list></ul></body></html>
<html><head><title>HelloFreemarker</title></head><body><h1>This is a simple example of Freemarker template engine.</h1><ul><li>Alice(20)</li><li>Bob(25)</li></ul></body></html>
<#-- greeting_en.ftl --><#--Use cap_first and upper_case built-ins toformat the name -->Hello ${name?cap_first} ${name?upper_case}!<#--Useif-else directive togenerate different messages based on gender --><#if gender =="male">You are a ${age} years old man.<#elseif gender =="female">You are a ${age} years old woman.<#else>You are a ${age} years old person.</#if><#--Useswitch directive togenerate different messages based on occupation --><#switch occupation><#case"student">You are studying hard for your future.<#break><#case"teacher">You are teaching the next generation.<#break><#case"doctor">You are saving lives every day.<#break><#case"engineer">You are building amazing things.<#break><#default>You are doing a great job.</#switch><#--Use hobby as a variable -->Your hobby is ${hobby}.
代码实现逻辑:
创建一个数据模型,用一个Map对象存储用户输入的姓名和年龄。
获取模板对象,调用process方法,将数据模型和模板文件合并,并输出到一个输出流中。
关闭输出流。
importfreemarker.template.Configuration;importfreemarker.template.Template;importfreemarker.template.TemplateException;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.io.Writer;importjava.util.HashMap;importjava.util.Locale;importjava.util.Map;importjava.util.Scanner;publicclassGreetingApp{publicstaticvoidmain(String[] args)throwsIOException,TemplateException{// 创建一个Scanner对象,用于接收用户输入Scanner scanner =newScanner(System.in);// 提示用户输入姓名、年龄、性别、爱好、职业和语言System.out.println("Please enter your name:");String name = scanner.nextLine();System.out.println("Please enter your age:");int age = scanner.nextInt();
scanner.nextLine();// consume the newline characterSystem.out.println("Please enter your gender (male/female/other):");String gender = scanner.nextLine();System.out.println("Please enter your hobby:");String hobby = scanner.nextLine();System.out.println("Please enter your occupation:");String occupation = scanner.nextLine();System.out.println("Please enter your language (en/cn/fr/de):");String language = scanner.nextLine();// 创建一个Configuration对象,设置模板文件所在的目录和编码方式Configuration cfg =newConfiguration(Configuration.VERSION_2_3_32);
cfg.setClassForTemplateLoading(GreetingApp.class,"/templates");
cfg.setDefaultEncoding("UTF-8");// 获取模板对象,根据用户输入的语言选择不同的模板文件Template template =null;switch(language){case"en":
template = cfg.getTemplate("greeting_en.ftl");break;case"cn":
template = cfg.getTemplate("greeting_cn.ftl");break;case"fr":
template = cfg.getTemplate("greeting_fr.ftl");break;case"de":
template = cfg.getTemplate("greeting_de.ftl");break;default:System.out.println("Invalid language. Use English as default.");
template = cfg.getTemplate("greeting_en.ftl");break;}// 创建一个数据模型,用一个Map对象存储用户输入的姓名、年龄、性别、爱好、职业和语言Map<String,Object> data =newHashMap<>();
data.put("name", name);
data.put("age", age);
data.put("gender", gender);
data.put("hobby", hobby);
data.put("occupation", occupation);
data.put("language", language);// 创建一个输出流Writer out =newOutputStreamWriter(System.out);// 将数据模型和模板文件合并,生成输出
template.process(data, out);// 关闭输出流
out.close();// 关闭Scanner对象
scanner.close();}}
运行这个小程序,可以得到以下的输出:
Please enter your name:TomPlease enter your age:18Please enter your gender (male/female/other):
male
Please enter your hobby:
reading
Please enter your occupation:
student
Please enter your language (en/cn/fr/de):
en
HelloTomTOM!You are a 18 years old man.
You are studying hard for your future.
Your hobby is reading.