报错信息:

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">
            <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/"/>-->
    </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

http://blog.csdn.net/guozy2015/article/details/70170342

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

2020-06-08 12:26:50 企业邮箱发送邮件时,若出现投递失败产生退信,内容提示包含如下: the mta server of * reply:550 failed to meet SPF requirements 或者 the mta server of 163.com — 163mx01.mxmail.netease.com(220.181.14.141) reply:550 MI:SPF mx14,QMCowECpA0qTiftVaeB3Cg—.872S2 1442548128 http://mail.163.com/help 301481