com.yuweix.kuafu.web.freemarker.FreemarkerUtil Maven / Gradle / Ivy
The newest version!
package com.yuweix.kuafu.web.freemarker;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.view.AbstractTemplateView;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* @author yuwei
*/
public class FreemarkerUtil {
private static Configuration cfg;
private static String ftlPath = "";
private FreemarkerUtil() {
}
public static String merge(String template) {
return merge(template, null);
}
public static String merge(String template, Map params) {
return merge(template, params, null, null);
}
public static String merge(String template, Map params, HttpServletRequest request, HttpServletResponse response) {
if (params == null) {
params = new HashMap();
}
if (request != null) {
params.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, new RequestContext(request
, response, request.getServletContext(), params));
}
try {
Template temp = cfg.getTemplate(ftlPath + template);
Writer out = new StringWriter();
temp.process(params, out);
out.flush();
return out.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void setCfg(Configuration cfg) {
FreemarkerUtil.cfg = cfg;
}
public void setFtlPath(String ftlPath) {
FreemarkerUtil.ftlPath = ftlPath;
}
}