-
/**
* Created: 2007-2-1
* Modified: 2007-2-1
*
* Author: Keven Chen
*/
package freemarker.loader;import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;import freemarker.cache.TemplateLoader;
/**
* @author Keven Chen
* @version $Revision 1.0 $
*
*/
public class StringTemplateLoader implements TemplateLoader {
private static final String DEFAULT_TEMPLATE_KEY = "_default_template_key";
private Map templates = new HashMap();
public StringTemplateLoader(String defaultTemplate){
if(defaultTemplate != null && !defaultTemplate.equals("")){
templates.put(DEFAULT_TEMPLATE_KEY, defaultTemplate);
}
}
public void AddTemplate(String name,String template){
if(name == null || template == null
|| name.equals("") || template.equals("")){
return ;
}
if(!templates.containsKey(name)){
templates.put(name, template);
}
}
public void closeTemplateSource(Object templateSource) throws IOException {
}public Object findTemplateSource(String name) throws IOException {
if(name == null || name.equals("")){
name = DEFAULT_TEMPLATE_KEY;
}
return templates.get(name);
}public long getLastModified(Object templateSource) {
return 0;
}public Reader getReader(Object templateSource, String encoding)
throws IOException {
return new StringReader((String) templateSource);
}}
- /**
- * Created: 2007-2-1
- * Modified: 2007-2-1
- *
- * Author: Keven Chen
- */
- package freemarker;
- import java.io.File;
- import java.io.OutputStreamWriter;
- import java.io.StringWriter;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import freemarker.loader.ClassPathTemplateLoader;
- import freemarker.loader.StringTemplateLoader;
- import freemarker.template.Configuration;
- import freemarker.template.Template;
- /**
- * @author Keven Chen
- * @version $Revision 1.0 $
- *
- */
- public class Test {
- public static void main(String[] args) throws Exception {
- Configuration cfg = new Configuration();
- cfg.setTemplateLoader(new StringTemplateLoader("欢迎:${user}"));
- cfg.setDefaultEncoding("UTF-8");
- Template template = cfg.getTemplate("");
- Map root = new HashMap();
- root.put("user", "Keven Chen");
- StringWriter writer = new StringWriter();
- template.process(root, writer);
- System.out.println(writer.toString());
- }
- }
FreeMarker解析字符串模板
最新推荐文章于 2024-03-15 13:44:31 发布