当前位置:首页 > Web开发 > 正文

freemarker在web应用项目的使用

2024-03-31 Web开发

FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。目前企业中:主要用Freemarker做静态页面或是页面展示。

以下都是网上摘要,感觉很有用,适合初学者了解

Freemarker的使用方法

把freemarker的jar包添加到工程中

<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>

首先需要添加freemarker.jar到项目,如果项目中有spring或者spirngmvc,需要整合,首先配置freemarkerConfig,代码结构如下:

<!-- 设置freeMarker的配置文件路径 --> <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property value="classpath:freemarker.properties" /> </bean> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property ref="freemarkerConfiguration" />
<property> <value>/WEB-INF/freemarker/</value> </property> <property><!--设置一些常用的全局变量--> <map> <entry key="xml_escape" value-ref="fmXmlEscape" /> <entry key="webRoot" value="/shop"></entry> <entry key="jsRoot" value="/shop/js"></entry> </map> </property> </bean>

其中一下代码是用来扫描.ftl的模板文件,在/web-info/freemarker目录中

<property> <value>/WEB-INF/freemarker/</value> </property>

然后freemarker用ftl文件来呈现视图,这时候就需要配置freemarker的视图解析器,代码如下:

<!-- 配置freeMarker视图解析器 --> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <property value="*.ftl" /> <property value="text/html; charset=utf-8" /> <property value="true" /> <property value="" /> <!-- <property value="true" /> <property value="true" /> <property value="true" /> --> <property value="0" /> </bean> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 通用解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property value="" /> <property value="*.html,*.jsp" /> <property value="" /> <property value="org.springframework.web.servlet.view.InternalResourceView" /> <property value="1"></property> </bean>

其中:<property value="0">代表了第一个匹配的是freemarker的视图解析器,如果匹配不成功,则自动选择order=1的其他解析器,目前的通用解析器可以解析.html跟.jsp的视图,如果需要其他视图的解析器,可以自行添加。

其中的exposeRequestAttributes  exposeSessionAttributes两个属性都被设置为true。结果是请求和会话属性都被复制到模板的属性集中,可以使用FreeMarker的表达式语言来访问并显示。

使用这些宏,必须设置FreeMarkerViewResolver的exposeSpringMacroHelpers属性为true

以上是freemarker与springmvc整合需要配置的xml文件。

下面来介绍一下在Java 代码中如何使用:

首先编写Freemarker的工具类,用来生成HTML文件的方法:

package com.hc.shop.common.tools; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig; import freemarker.template.Template; import freemarker.template.TemplateException; /** * @author HuifengWang 静态化方法 **/ public class FreeMarkerUtil { /** * * 生成HTML静态页面的公公方法 * @param fmc * @param templateName 模板的名称 * @param request * @param map 生成模板需要的数据 * @param filePath 相对于web容器的路径 * @param fileName 要生成的文件的名称,带扩展名 * @author HuifengWang * */ public static void createHtml(FreeMarkerConfig fmc, String templateName, HttpServletRequest request, Map<?, ?> map, String filePath, String fileName) { Writer out = null; try { Template template = fmc.getConfiguration() .getTemplate(templateName); String htmlPath = request.getSession().getServletContext() .getRealPath(filePath) + "http://www.mamicode.com/" + 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(map, 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(); } } } /** * @param request * @param filePath 文件存放的路径 * @param fileName 文件的名称,需要扩展名 * @author HuifengWang * @return */ public static Map<String,Object> htmlFileHasExist(HttpServletRequest request,String filePath, String fileName) { Map<String,Object> map = new HashMap<String,Object>(); String htmlPath = request.getSession().getServletContext() .getRealPath(filePath) + "http://www.mamicode.com/" + fileName; File htmlFile = new File(htmlPath); if(htmlFile.exists()){ map.put("exist", true); }else{ map.put("exist",false); } return map ; } }

以上就是要生成HTML文件的工具类,参数注解都有,应该很好理解。

如何在Controller中调用??下面来看一个很简单的demo

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/38851.html