freemaker使用json形式的变量渲染字符串及字节数组

/**
 * freemaker, 使用json形式变量渲染字符串
 */
@Test
public void testRenderString() {
    try {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();

        stringTemplateLoader.putTemplate("test", "<#assign json='${jsonString}'?eval />${json.name}", 1L);
        configuration.setTemplateLoader(stringTemplateLoader);
        configuration.setEncoding(Locale.CHINA, "UTF-8");

        Template template = configuration.getTemplate("test", Locale.CHINESE, "UTF-8");

        Map<String, String> paramsMap = new HashMap<>(1);
        StringWriter out = new StringWriter();
        Gson gson = new Gson();
        Map<String, String> map = new HashMap<>();
        map.put("name", "测试");
        paramsMap.put("jsonString", gson.toJson(map));

        template.process(paramsMap, out);
        String result = out.toString();
        System.out.println("渲染:" + result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * freemaker, 使用json形式变量渲染字节数组
 */
@Test
public void testRenderByteArray() {
    try {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
        ByteArrayTemplateLoader byteArrayTemplateLoader = new ByteArrayTemplateLoader();
        //注意,此处的html内容不允许转义,否则渲染会失败.报错jsonnull(实际是找不到assign标签定义的json)
        //如果页面传输到后台的是转义过的,可以使用spring框架的工具类进行反转义HtmlUtils.htmlUnescape
        byteArrayTemplateLoader.putTemplate("test", "<#assign json=\"${jsonString}\"?eval /><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>测试html</title></head><body>hello${json.name}<br/></body></html>".getBytes("UTF-8"), 1L);
        configuration.setTemplateLoader(byteArrayTemplateLoader);
        configuration.setEncoding(Locale.CHINA, "UTF-8");

        Template template = configuration.getTemplate("test", Locale.CHINESE, "UTF-8");

        Map<String, String> paramsMap = new HashMap<>(1);
        StringWriter out = new StringWriter();
        Gson gson = new Gson();
        Map<String, String> map = new HashMap<>();
        map.put("name", "测试");
        //此处定义变量jsonStringassign使用
        paramsMap.put("jsonString", gson.toJson(map));

        template.process(paramsMap, out);
        String result = out.toString();
        System.out.println("渲染:" + result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

以上处理比较麻烦,其实freemaker直接处理的还是map或者java Bean.

下面改为使用map形式的变量

@Test
public void testRenderString() {
    try {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();

        stringTemplateLoader.putTemplate("test", "${name}${student.name}<#list cc.studentList as ss>${ss.name}</#list>", 1L);
        configuration.setTemplateLoader(stringTemplateLoader);
        configuration.setEncoding(Locale.CHINA, "UTF-8");

        Template template = configuration.getTemplate("test", Locale.CHINESE, "UTF-8");

        TT tt = new TT();

        StringWriter out = new StringWriter();
        Gson gson = new Gson();
        String json = gson.toJson(tt);
        System.out.println("json=="+json);
        Map<String, Object> params = gson.fromJson(json, new TypeToken<HashMap<String, Object>>() {
        }.getType());
        template.process(params, out);
        String result = out.toString();
        System.out.println("渲染:" + result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33315102/article/details/80742447