报错信息:
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
按照这个博文解决了: http://blog.csdn.net/guozy2015/article/details/70170342
但是为什么要放在classpath下面呢,我试过classpath:/../WEB-INF/freeMarker,第一次可以,后面又莫名其妙的不行了。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。