问题描述
没有整合其他框架,FreeMarker独自运行出现模板文件找不到问题求救啊
解决方案
本帖最后由 java_yinxuan 于 2014-07-10 17:25:07 编辑
解决方案二:
tpl模板应该放在/WEB-INF/classes目录下,而不是lib目录下
解决方案三:
web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!--FreemarkerinServlet--><servlet><servlet-name>freemarker</servlet-name><servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class><init-param><param-name>TemplatePath</param-name><param-value>/</param-value></init-param><init-param><param-name>NoCache</param-name><param-value>true</param-value></init-param><init-param><param-name>ContentType</param-name><param-value>text/html;charset=UTF-8</param-value></init-param><init-param><param-name>template_update_delay</param-name><param-value>0</param-value></init-param><init-param><param-name>default_encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>number_format</param-name><param-value>0.
解决方案四:
解决方案五:
解决方案六:
#</param-value></init-param></servlet><servlet-mapping><servlet-name>freemarker</servlet-name><url-pattern>*.htm</url-pattern></servlet-mapping><servlet><servlet-name>test</servlet-name><servlet-class>servlet.MyServlet</servlet-class></servlet><servlet-mapping><servlet-name>test</servlet-name><url-pattern>/test</url-pattern></servlet-mapping></web-app>
解决方案七:
使用FreeMarker的Servletimportbean.User;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;publicclassMyServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{Useruser=newUser("HuangBiao","password");req.setAttribute("user",user);req.getRequestDispatcher("/WEB-INF/view/freemarker/fm.htm").forward(req,resp);//这里可以看到fm.htm放的路径,这个是模板文件}}
解决方案八:
//设置模版路径freemarker.template.Configuration.setDirectoryForTemplateLoading(newFile(ftlpath));
解决方案九:
引用楼主java_yinxuan的回复:
没有整合其他框架,FreeMarker独自运行出现模板文件找不到问题求救啊
我的代码中这样设置的cfg.setServletContextForTemplateLoading(getServletContext(),"WEB-INF/templates");
路径不应该这样写吗?
解决方案十:
引用1楼Inhibitory的回复:
tpl模板应该放在/WEB-INF/classes目录下,而不是lib目录下
我的代码中这样设置的cfg.setServletContextForTemplateLoading(getServletContext(),"WEB-INF/templates");
解决方案十一:
我只会用我说的这种方式,你的那种没用过。
解决方案十二:
引用4楼maihao110的回复:
//设置模版路径freemarker.template.Configuration.setDirectoryForTemplateLoading(newFile(ftlpath));
这样配置不行吗?cfg.setServletContextForTemplateLoading(getServletContext(),"WEB-INF/templates");
解决方案十三:
引用2楼Inhibitory的回复:
web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!--FreemarkerinServlet--><servlet><servlet-name>freemarker</servlet-name><servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class><init-param><param-name>TemplatePath</param-name><param-value>/</param-value></init-param><init-param><param-name>NoCache</param-name><param-value>true</param-value></init-param><init-param><param-name>ContentType</param-name><param-value>text/html;charset=UTF-8</param-value></init-param><init-param><param-name>template_update_delay</param-name><param-value>0</param-value></init-param><init-param><param-name>default_encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>number_format</param-name><param-value>0.
解决方案十四:解决方案十五:
解决方案:
#</param-value></init-param></servlet><servlet-mapping><servlet-name>freemarker</servlet-name><url-pattern>*.htm</url-pattern></servlet-mapping><servlet><servlet-name>test</servlet-name><servlet-class>servlet.MyServlet</servlet-class></servlet><servlet-mapping><servlet-name>test</servlet-name><url-pattern>/test</url-pattern></servlet-mapping></web-app>
配置<init-param></init-param>是必须的吗?我刚接触FreeMarker,没有整合任何框架,只单纯用FreeMarker