我用Freemarker模板编写了一个Java程序,但它显示已弃用的错误。我无法理解。

3
import java.io.*;
import java.util.*;
import freemarker.template.*;

public class HelloFreemarker {

    public static void main(String[] args)  
            throws IOException, TemplateException {
        Configuration cfg = new Configuration();
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        cfg.setDirectoryForTemplateLoading(new File("."));
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("name", "World");
        Template template = cfg.getTemplate("hello.ftl");
        template.process(model, 
        new OutputStreamWriter(System.out));
    }
}

 hello ${name}!

我使用Freemarker模板编写了一个Java程序,但在尝试编译/构建程序时出现配置错误。一条消息显示配置已过时。我正在使用JDK 8和JRE 8,并使用Eclipse Neon作为我的IDE。请帮助我执行该程序。

enter image description here


查看已弃用的方法/类的javadoc。它几乎肯定会指引你使用替代方案的方向。 - Henrik Aasted Sørensen
更新您的Freemarker库到新版本。 - user4856296
2个回答

7
这是因为从版本2.3.21开始,Configuration()构造函数已被弃用。
使用新的带参数构造函数Configuration(Version)来实例化Configuration对象。
根据freemarker API文档,Version是一个类,用于指定您要应用哪些不完全向后兼容的修复程序版本。
例如:假设我正在使用freemarker-2.3.28.jar并且想启用其所有向后兼容的错误修复/改进,则可以按以下方式创建Configuration对象: Configuration cofig = new Configuration(Configuration.VERSION_2_3_28); 所有freemarker API版本都可以在此处检查。希望这有所帮助 :)

0

这可能是因为在创建配置管理器时没有指定值。我在Apache网站上找到了一个示例:

Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);

显然,当留空时,它会选择默认值。此默认值然后调用已弃用的函数。

您应该确定要使用哪个版本的配置;可能是最新的稳定版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接