Template not found for name "sample1.ftl"

Template not found for name "sample1.ftl"

package com.imooc.freemarker;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
public class Sample {
    public static void main(String[] args) throws TemplateNotFoundException,IOException, TemplateException {
        Configuration config = new Configuration(Configuration.VERSION_2_3_31);
        config.setClassForTemplateLoading(Sample.class,"");
        Template t = config.getTemplate("sample1.ftl");
        Map<String,Object> data = new HashMap<String,Object>();
        data.put("baidu","baidu");
        data.put("url","https://www.baidu.com");
        t.process(data,new OutputStreamWriter(System.out));
    }
}


${site}-${url}


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.imooc</groupId>
    <artifactId>imooc-oa</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>
    </dependencies>
</project>

https://img1.sycdn.imooc.com//climg/624abcef09869bad20540648.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小尤 2022-04-04 18:29:52

同学你好,根据异常信息提示,提示没有找到sample1.ftl文件,建议同学在对应目录中查看sample1.ftl文件是否存在。

祝学习愉快!

  • 提问者 晓舟 #1


    https://img1.sycdn.imooc.com//climg/624aca57095f4a7506950476.jpg


    where's the error?

    2022-04-04 18:37:30
  • 好帮手慕小尤 回复 提问者 晓舟 #2

    同学你好,同学该项目是一个web项目,则建议同学设置web模块,然后将ftl文件放置到webapp目录下。如下所示:

    https://img1.sycdn.imooc.com//climg/624cf41509d38b2603490701.jpg

    https://img1.sycdn.imooc.com//climg/624cf4d70924911410150543.jpg

    祝学习愉快!

    2022-04-06 10:03:06
  • 提问者 晓舟 回复 好帮手慕小尤 #3

    "C:\Program Files\Java\jdk1.8.0_162\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar=60459:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_162\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\rt.jar;C:\Users\dxz-DC\eclipse-workspace\FreemarkerSample\target\classes;C:\Users\dxz-DC\.m2\repository\org\freemarker\freemarker\2.3.31\freemarker-2.3.31.jar" com.imooc.freemarker.FreemarkerSample

    Exception in thread "main" freemarker.template.TemplateNotFoundException: Template not found for name "sample.ftl".

    The name was interpreted by this TemplateLoader: ClassTemplateLoader(resourceLoaderClass=com.imooc.freemarker.FreemarkerSample, basePackagePath="" /* relatively to resourceLoaderClass pkg */).

    at freemarker.template.Configuration.getTemplate(Configuration.java:2883)

    at freemarker.template.Configuration.getTemplate(Configuration.java:2685)

    at com.imooc.freemarker.FreemarkerSample.main(FreemarkerSample.java:16)


    Process finished with exit code 1

    https://img1.sycdn.imooc.com//climg/624d0118097eb8a525601148.jpg

    操作了,仍然不行


    2022-04-06 10:55:36
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师