使用Freemarker实现网页静态化 中文乱码

public class TestFreemarker {

    @Test
    public void testFreemarkerFirst() throws Exception {
        // 创建一个Configuration对象
        Configuration configuration = new Configuration(Configuration.getVersion());
        // 设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File("F:/ftl"));
        // 设置模板字符集
        configuration.setDefaultEncoding("UTF-8");
        // 加载模板文件
        Template template = configuration.getTemplate("first.htm");
        // 创建一个数据集
        Map data = new HashMap();
        // data.put("hello", "Hello Freemarker!!!");
        data.put("title", "Hello Freemarker!!!");
        data.put("stu", new Student(1, "xm", 18, "温都水城"));

        // 设置模板输出的目录及输出的文件名
        // FileWriter writer = new FileWriter(new File("F:/temp/freemarker/hello.html"));
        
        FileWriter writer = new FileWriter(new File("F:/ftl/first.html"));
        //乱码解决
        FileWriterWithEncoding writer2 = new FileWriterWithEncoding(new File("F:/ftl/first.html"), 
        "UTF-8");
        // 生成文件
        template.process(data, writer2);
        // 关闭流
        writer.close();
    }

}

猜你喜欢

转载自blog.csdn.net/qq_32534441/article/details/87701677