freemark輸出字串

我就是曹總發表於2019-05-11

java

package com.freemark.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreeMarkTest {
	private Configuration cfg;
	public void init()throws Exception {
		cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File("D:\\"));
	}
	
	public  String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
	
	public void process()throws Exception
	{
	        Map root = new HashMap();
	        root.put("classname", "jiajia");
	        root.put("methodName" , "SockManage");
	        Template t = cfg.getTemplate("daoInterface.ftl");
	        File afile = new File("D:\\b.java");
			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));
			 // 輸出流  
	        StringWriter writer = new StringWriter();  
	        // 將資料和模型結合生成html  
	        t.process(root, writer);  
//			t.process(root, out);
			String resultString = writer.toString();  
			System.out.print(replaceBlank(resultString)+"jiajia");
//			t.process(propMap, out);	
	        
	       
//	       / String a=t.getRootTreeNode().getNodeName();
//	        System.out.println(t+"   ====="+a);
	}
	public static void main(String[] args) throws Exception {
		FreeMarkTest hf = new FreeMarkTest();
		   hf.init();
		   hf.process();
	}
	
}




模板

<#if classname=='jiajia'>全部頻道
<#elseif classname=='linpeng'>撫州一套
<#elseif classname=='zhipeng'>撫州二套
</#if> 




內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。

相關文章