freemarker: Template not found for name "xxx.ftl". 5C

报错信息:

 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

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!