FreeMarker 真实案例应用
定义模板
这个模板很简单,就是定义了一个显示文章的指今,然在的显示最新文章和热门文章的时候调用即可
模板文件源代码:
<#macro showNewArticles articles> <#list articles as article> <li> ${article_index+1}. <a target="_blank" href="/article/${article.id }">${article.title }</a> </li> </#list></#macro><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><link rel="stylesheet" type="text/css" href="/resources/css/foot.css" /><div id="footer_area"> <div id="footer_area_content"> <div name="code">package com.naxsu.service; import java.util.HashMap;import java.util.List;import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.naxsu.entity.Article;import com.naxsu.utils.FreemarkerUtil;@Service("freemarkerService")public class FreeMarkerService { @Resource(name="articleService") private ArticleService articleservice; public void getFootPage() { Map<String,Object> root = new HashMap<String,Object>(); List<Article> newDESCArticles = articleservice.getNewDESCArticle(); List<Article> viewCountDescArticles = articleservice.getViewCountDESCArticle(); root.put("newDESCArticles", newDESCArticles); root.put("viewCountDescArticles", viewCountDescArticles); String path = this.getClass().getProtectionDomain() .getCodeSource().getLocation().getPath(); path = path.substring(0,path.indexOf("/WEB-INF")); FreemarkerUtil.getInstance().fprint("foot.ftl", root, path+"/WEB-INF/jsp/template/foot.jsp"); } }
<aop:config> <aop:aspect ref="freemarkerService"> <aop:pointcut id="createFootTemplate" expression="execution(* com.naxsu.service.ArticleService.insert*(..))|| execution(* com.naxsu.service.ArticleService.update*(..))|| execution(* com.naxsu.service.ArticleService.delete*(..))"/> <aop:after method="getFootPage" arg-names="id" pointcut-ref="createFootTemplate"/> </aop:aspect></aop:config>