(一) FreeMarker的优点
- 可以彻底的分离表现层和业务逻辑。曾经在使用JSP 开发过程中发现在页面中大量的存在业务逻辑的代码,使得页面内容凌乱,在后期大量的修改维护过程中就变得非常困难。但是,**FreeMarker不支持Java脚本代码。**FreeMarker的原理就是:模板+数据模型=输出,模板只负责数据在页面中的表现,不涉及任何的逻辑代码,而所有的逻辑都是由数据模型来处理的。用户最终看到的输出是模板和数据模型合并后创建的。
2.**FreeMarker可以提高开发效率。**根据以往的开发经验,使用的都是JSP 页面来展示数据的,即所谓的表现层。大家都知道,JSP在第一次执行的时候需要转换成Servlet类,开发阶段进行功能调适时,需要频繁的修改JSP,每次修改都要编译和转换,那么试想一天中我们浪费在程序编译的时间有多少。相对于JSP来说,FreeMarker模板技术不存在编译和转换的问题,所以就不会存在上述问题。而且开发过程中,大家在不必在等待界面设计开发人员完成页面原形后,大家再来开发程序。
3 . **FreeMarker使得开发过程中的人员分工更加明确。**以往用JSP展现数据时,作为程序员的我们,并不熟悉界面设计技术,反之界面开发人员,也并不熟悉程序语言。对两者而言,交替性的工作本身就有难度。有时候稍有不慎,可能会将某个页面元素删除或去掉了某个程序符号,使得页面走样或程序错误,这样就需要双方相互沟通协作,解决出现的问题。有时候因为项目中的时间,任务量等因素的存在,可能这个工作就由一个人来完成,这样就可能加大某一方开发人员的工作量。使用FreeMarker后,作为界面开发人员,只专心创建HTML文件、图像以及Web页面的其他可视化方面,不用理会数据;而程序开发人员则专注于系统实现,负责为页面准备要显示的数据。
(二)FreeMarker的缺点
1.FreeMarker模板技术,在修改模板后,可能会看到已经过期的数据。如:生成静态的HTML页面后,如果一旦模板改变,而没有及时更新模板生成的HTML页面的话,用户看到的就是过期的数据。
2.FreeMarker模板技术在应用过程中,FreeMarker中的变量必须要赋值,如果不赋值,那么就会抛出异常。想避免错误就要应用if/elseif/else 指令进行判段,如果对每一个变量都判断的话,那么则反而增加了编程的麻烦。
3.FreeMarker的map限定key必须是string,其他数据类型无法操作。
4.FreeMarker不支持集群应用。为了编成的方便性,把序列化的东西都放到了Session中,如Session,request等,在开发的过程中确实方便,但如果将应用放到集群中,就会出现错误。
(三)配置pom文件,引入所需jar包
<!-- FreeMarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker-gae</artifactId>
<version>2.3.25-incubating</version>
</dependency>
(四)后台源码
/**
* 首页静态化
* 首页有人访问直接生成html文件,如果存在则直接访问
*/
@Controller
@RequestMapping
public class index extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
private ChannelService channelService;
@Autowired
private ContentService contentService;
@Autowired
private AdvertisementService advertisementService;
@RequestMapping
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
String saveDir = request.getServletContext().getRealPath(FreeMarkerConstants.index_dir);
String indexPath = request.getServletContext()
.getRealPath(FreeMarkerConstants.index_dir + FreeMarkerConstants.index_html);
File file = new File(saveDir);
if (!file.exists()) {
file.mkdirs();
}
file = new File(indexPath);
if (!file.exists()) {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
String templatePath = this.getClass().getClassLoader().getResource("/templates/index").getPath();
configuration.setDirectoryForTemplateLoading(new File(templatePath));
configuration.setDefaultEncoding("UTF-8");
Map<String, Object> dataMap = new HashMap<String, Object>();
List<Advertisement> advertisements = this.getAdvertisements();
List<Channel> praentChannelList = this.getData();
List<Content> stickContent = this.getStickContent();
List<Content> subCourseContent = this.getStickCourseCourse();
List<Content> stickExpertContent = this.getStickExpertContent();
List<Content> stickActivityContent = this.getStickActivityContent();
List<Content> stickGeneralContent = this.getStickGeneralContent();
base ctx = this.initPath(request, response);
dataMap.put("advertisements", advertisements);
dataMap.put("praentChannelList", praentChannelList);
dataMap.put("stickContent", stickContent);
dataMap.put("subCourseContent", subCourseContent);
dataMap.put("stickExpertContent", stickExpertContent);
dataMap.put("stickActivityContent", stickActivityContent);
dataMap.put("stickGeneralContent", stickGeneralContent);
dataMap.put("ctx", ctx);
Template template = configuration.getTemplate("index.ftl");
try {
Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
template.process(dataMap, writer);
writer.flush();
writer.clos