创建配置时出错,无法解析 setClassForTemplateLoading() 和 getTemplate()

error when creating configuration and can't resolve setClassForTemplateLoading() and getTemplate()

我是 IntelliJ IDEA 的新手,正在尝试 freemarker 和 maven。 我刚刚安装了那个软件并尝试了一个 freemarker 项目,它显示了覆盖配置对象创建的方法。如果我这样做,我就不能使用 setClassForTemplateLoading()。它表明无法解决这个问题。还有 getTemplate()。

public class HelloWorldFreemarkerStyle {
public static void main(String[] args) {
    Configuration cfg = new Configuration();//shows error, insisting to use override method
    cfg.setClassForTemplateLoading(HelloWorldFreemarkerStyle.class, "/");//can't resolve

    try {
        Template helloTemplate = cfg.getTemplate("hello.ftl");// can't resolve
        StringWriter writer = new StringWriter();
        Map<String, Object> helloMap = new HashMap<String, Object>();
        helloMap.put("name", "Vicky");
        helloTemplate.process(helloMap, writer);
        System.out.println(writer);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

freemarker 依赖项:

<dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.20</version>
</dependency>

hello.ftl:

<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>hello ${name}</h1>
</body>
</html>

确保您正在导入

import freemarker.template.Configuration;

在文件的顶部,而不是

import javax.security.auth.login.Configuration;