开发者社区> 问答> 正文

freemarker: Template not found for name ?400报错

报错信息:

freemarker.template.TemplateNotFoundException: Template not found for name "login.ftl".
The name was interpreted by this TemplateLoader: MultiTemplateLoader(loader1 = org.springframework.ui.freemarker.SpringTemplateLoader@3d5c089b, loader2 = ClassTemplateLoader(resourceLoaderClass=org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer, basePackagePath="" /* relatively to resourceLoaderClass pkg */)).
	at freemarker.template.Configuration.getTemplate(Configuration.java:1833)
	at freemarker.template.Configuration.getTemplate(Configuration.java:1646)
	at com.csaarg.util.framework.FreeMarkerUtil.create(FreeMarkerUtil.java:35)
	at com.csaarg.sharedvip.controller.ForwardController.login(ForwardController.java:33)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)

FreeMarker 配置:

<bean id="freeMarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="defaultEncoding" value="UTF-8"/>
    <!-- 模板路徑 -->
    <property name="templateLoaderPath" value="/WEB-INF/freeMarker/"/>
    <!-- 配置文件 -->
    <property name="freemarkerSettings" ref="freemarkerSettings"/>
    <!-- 自定義指令 -->
    <property name="freemarkerVariables">
        <map>
            <entry key="xml_escape" value-ref="fmXmlEscape"/>
            <!--<entry key="view_url" value="http://localhost:8299/Store/content/css/"/>-->
            <!--<entry key="JS_URL" value="http://localhost:8299/Store/content/js/"/>-->
        </map>
    </property>
</bean>

<!-- 配置文件 -->
<bean id="freemarkerSettings" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:properties/freemarker.properties"/>
</bean>
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>

<!--識圖解析器-->
<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
    <property name="viewNames" value="*.ftl"/>
    <property name="contentType" value="text/html; charset=utf-8"/>
    <property name="cache" value="true"/>
    <property name="suffix" value=""/>
    <!-- 	<property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="exposeSpringMacroHelpers" value="true" /> -->
    <property name="order" value="0"/>
</bean>
<!--对模型视图名称的解析,即在模型视图名称添加前后缀 通用解析器-->
<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value=""/>
    <property name="viewNames" value="*.html"/>
    <property name="suffix" value=""/>
    <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"/>
    <property name="order" value="1"></property>
</bean>

Controller:

/**
 * 转发
 */
@Controller
public class ForwardController {

    @Autowired
    private static FreeMarkerConfigurer freeMarkerConfigurer;

    @Autowired
    private FreeMarkerConfig freeMarkerConfig;

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpServletRequest request) {

        FreeMarkerUtil.create(request, freeMarkerConfig, "login.ftl", null, "/WEB-INF/freeMarker", "login.html");

        return "/view/login.html";
    }

}

工具类:

public class FreeMarkerUtil {

    private FreeMarkerUtil() {
    }

    /**
     * 生成 HTML 靜態頁面
     *
     * @param request
     * @param freeMarkerConfig
     * @param templateName     模板的名稱
     * @param model            生成模板需要的數據
     * @param filePath         相對於 web 容器的路徑
     * @param fileName         要生成的文件的名稱,帶擴展名
     */
    public static void create(HttpServletRequest request, FreeMarkerConfig freeMarkerConfig, String templateName,
                              Map<String, Object> model, String filePath,
                              String fileName) {
        Writer out = null;
        try {
            Template template = freeMarkerConfig.getConfiguration()
                    .getTemplate(templateName);
            String htmlPath = request.getSession().getServletContext()
                    .getRealPath(filePath)
                    + "/" + fileName;
            File htmlFile = new File(htmlPath);
            if (!htmlFile.getParentFile().exists()) {
                htmlFile.getParentFile().mkdirs();
            }
            if (!htmlFile.exists()) {
                htmlFile.createNewFile();
            }
            out = new OutputStreamWriter(new FileOutputStream(htmlPath), "UTF-8");
            template.process(model, out);
            out.flush();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                out = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

webapp 目录结构:

报错行:

Template template = freeMarkerConfig.getConfiguration().getTemplate(templateName);

参照配置:https://my.oschina.net/HuifengWang/blog/300461

展开
收起
爱吃鱼的程序员 2020-06-08 12:26:39 4197 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    按照这个博文解决了: http://blog.csdn.net/guozy2015/article/details/70170342

    但是为什么要放在classpath下面呢,我试过classpath:/../WEB-INF/freeMarker,第一次可以,后面又莫名其妙的不行了。

    2020-06-08 12:26:50
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Apache Wicket User Guide - Ref 立即下载
OPEN SOURCE IN A DATA-DRIVEN WORLD 立即下载
Custom applicationgs 立即下载