FreeMarker基础知识 标签与使用
?????? <#assign price = 42 />
?????? ${price}
?????? ${price?string}
?????? ${price?string.number}
?????? ${price?string.currency}
?????? ${price?string.percent}
????????????? ${x}
??????? </#list>
???????????????? ${test?html}
????????? ${test?upper_case?html}
??????????????
???? 6、noparse指令
???????? noparse指令指定FreeMarker不处理该指令里包含的内容,该指令的语法格式如下:
???????? <#noparse>
??????????? ...
???????? </#noparse>
?????????
???? 7、escape、noescape指令
???????
???????
???? 8、assign指令
??????? 它用于为该模板页面创建或替换一个顶层变量
????????
???? 9、setting指令
??????? 该指令用于设置FreeMarker的运行环境,该指令的语法格式如下:
??????? <#setting name = value>
??????? name 的取值范围包括如下几个
???????? locale :该选项指定该模板所用的国家/语言选项
???????? number_format:该选项指定格式化输出数字的格式
???????? boolean_format:该选项指定两个布尔值的语法格式,默认值是"true、false"
???????? date_format,time_format,datetime_format:该选项指定格式化输出日期的格式
???????? time_zone:? 设置格式化输出日期时所使用的时区
???? 10、macro、nested、return指令
?
下面再介绍一个例子
Java代码
public class TemplateTest {??
??
??? /**?
???? * @param args?
???? */??
??? public static void main(String[] args) throws Exception{??
??????? /* 准备数据 */??
??????? Map latest = new HashMap();??
??????? latest.put("url", "products/greenmouse.html");??
??????? latest.put("name", "green mouse");??
??????????
??????? Map root = new HashMap();??
??????? root.put("user", "Big Joe");??
??????? root.put("latestProduct", latest);??
??????? root.put("number", new Long(2222));??
??????? root.put("date",new Date());??
??????????
??????? List listTest = new ArrayList();??
??????? listTest.add("1");??
??????? listTest.add("2");??
??????????
??????? root.put("list",listTest);??
??????????
??????? TemplateEngine freemarkerEngine = (TemplateEngine)TemplateFactory.getInstance().getBean("freemarker");??
??????? freemarkerEngine.run(root);//使用freemarker模板技术??
??????????
??????? TemplateEngine velocityEngine = (TemplateEngine)TemplateFactory.getInstance().getBean("velocity");??
??????? velocityEngine.run(root);//使用velocity模板技术??
??? }??
??
}?
?
工厂类,用来得到模板引擎
Java代码
public class TemplateFactory {??
??? private static TemplateFactory instance;??
??? private Map objectMap;??
??????
??? static{??
??????? instance = new TemplateFactory();??
??? }??
??????
??? public TemplateFactory() {??
??????? super();??
??????? this.objectMap = new HashMap();??
??????? synchronized (this) {??
??????????? objectMap.put("freemarker", new FreemarkerTemplateEngine(){??
??????????????? public String getTemplatePath() {??
??????????????????? return "template";??
??????????????? }??
??????????? });??
??????????????
??????????? objectMap.put("velocity", new VelocityTemplateEngine());??
??????? }??
??? }??
??
??? public static TemplateFactory getInstance(){??
??????? return instance;??
??? }??
??????
??? /**?
???? * 模仿spring的工厂?
???? * @param beanName?
???? * @return?
???? */??
??? public Object getBean(String beanName){??
??????? return objectMap.get(beanName);??
??? }??
??
}?
?
引擎接口
Java代码
public interface TemplateEngine {??
??????
??? void run(Map context)throws Exception;??
??
}?
?
模板引擎的实现使用method template模式,因为有两个实现,这两个实现又存在公共的逻辑,所以选择了这个模式
Java代码
public abstract class AbstractTemplateEngine implements TemplateEngine{??
??
??? public abstract String getTemplatePath();??
??????
??? public abstract String getTemplate();??
??????
??? public abstract String getEngineType();??
??????
??? public void run(Map context)throws Exception{??
??????? if(Constants.ENGINE_TYPE_FREEMARKER.equals(getEngineType()))??
??????????? executeFreemarker(context);??
??????? else??
??????????? executeVelocity(context);??
??? }??
??????
??? private void executeFreemarker(Map context)throws Exception{??
??????? Configuration cfg = new Configuration();??
??????? cfg.setDirectoryForTemplateLoading(??
??????????????? new File(getTemplatePath()));??
??????? cfg.setObjectWrapper(new DefaultObjectWrapper());??
??????????
??????? cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250));??
??????????????????
??????? Template temp = cfg.getTemplate(getTemplate());??
??
??????? Writer out = new OutputStreamWriter(System.out);??
??????? temp.process(context, out);??
??????? out.flush();??
??? }??
??????
??? private void executeVelocity(Map root)throws Exception{??
??????????
??????? Velocity.init();??
??????? VelocityContext context = new VelocityContext(root);??
??????? org.apache.velocity.Template template = null;??
??????????
??????? template = Velocity.getTemplate(getTemplatePath()+getTemplate());??
??????????
??????? StringWriter sw = new StringWriter();??
??????? template.merge( context, sw );??
??????? System.out.print(sw.toString());??
??
??? }??
??
}?
?
这个是freemarker实现
Java代码
?
public class FreemarkerTemplateEngine extends AbstractTemplateEngine{??
??? private static final String DEFAULT_TEMPLATE = "FreemarkerExample.ftl";??
??????
??? /**?
???? * 这个方法应该实现的是读取配置文件?
???? */??
??? public String getTemplatePath() {??
??????? return null;??
??? }??
??????
??? public void run(Map root) throws Exception{??
??????? super.run(root);??
??? }??
??
??? public String getTemplate() {??
??????? // TODO Auto-generated method stub??
??????? return DEFAULT_TEMPLATE;??
??? }??
??
??? public String getEngineType() {??
??????? return Constants.ENGINE_TYPE_FREEMARKER;??
??? }??
}
?
这个是velocity实现
Java代码
?public class VelocityTemplateEngine extends AbstractTemplateEngine{??
??
private static final String DEFAULT_TEMPLATE = "VelocityExample.vm";??
??
??? public String getTemplatePath() {??
??????? return "/template/";??
??? }??
??????
??? public void run(Map map) throws Exception{??
??????? super.run(map);??
??? }??
??
??? public String getTemplate() {??
??????? // TODO Auto-generated method stub??
??????? return DEFAULT_TEMPLATE;??
??? }??
??
??? public String getEngineType() {??
??????? // TODO Auto-generated method stub??
??????? return Constants.ENGINE_TYPE_VELOCITY;??
??? }??
}
?
以下是模板?
1,freemarker模板?
Java代码
freemarker template test:??
string test-----------${user}-----------${number}-----------${latestProduct.url}-----------${latestProduct.name}??
condition test-----------??
<#if user == "Big Joe">??
list iterator-----------??
<#list list as aa>??
${aa}??
</#list>???
</#if>??
date test-----------${date?string("MMM/dd/yyyy")}????
2,velocity模板
Java代码??
******************************************************************************************************************??
velocity template test:??
string test-----------${user}-----------${number}-----------${latestProduct.url}-----------${latestProduct.name}??
condition test-----------??
#if ($user == "Big Joe")??
list iterator-----------??
#foreach( $aa in $list )??
$aa??
#end??
#end??
date test-----------${date}?
至此整个例子就结束了,这个例子比较直观的表现两种技术的应用