`
huaxin803
  • 浏览: 108395 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

FreeMarker在领悟教程网实战应用

阅读更多
摘要: 上一节我们整理了FreeMarker与SpringMVC整合,这一节学习在领悟书生教程网中是怎样实现应用freemarker静态化页面(减少数据库查询)的。需求是这样的,我页面的底部,有“最新文章”和“热门文章”。因为每个页面都有这个显示,每次访问都要查询数据库,这不是太合理,所以在添加文章、修改文章和删除文章的时候,都查询数据库,生成一个jsp页面,然后嵌套在页面中。

上一节我们整理了FreeMarker与SpringMVC整合,这一节学习在领悟书生教程网中是怎样实现应用freemarker静态化页面(减少数据库查询)的。

这一节的标题我们就叫做FreeMarker在领悟教程网实战应用,需求是这样的,我页面的底部,有“最新文章”和“热门文章”。因为每个页面都有这个显示,每次访问都要查询数据库,这不是太合理,所以在添加文章、修改文章和删除文章的时候,都查询数据库,生成一个jsp页面,然后嵌套在页面中。


定义模板

这个模板很简单,就是定义了一个显示文章的指今,然在的显示最新文章和热门文章的时候调用即可

模板文件源代码:

<#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 class="box">
            <h3>最近发布</h3>
        <div class="box_content">
        <ol>
                <@showNewArticles articles=newDESCArticles />
        </ol>
        </div>
    </div>
  
    <div class="box">
        <h3>热门文章</h3>
        <div class="box_content">
                <ol>
                <@showNewArticles articles=viewCountDescArticles />
            </ol>
        </div>
    </div>
    <!-- 省略了一部分 -->
    </div>
</div>

Freemarker的服务类

freemarker服务类会调用articleService

完整源代码:

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");
    }
      
}

用spring aop实现切面拦截文章的增删改的服务

在增删改文章的时候都会调用freemarkerService,从而生成相应的jsp。FreeMarker与SpringMVC整合在这里不做多说,请看相关的文章。

源代码如下:

<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>

jsp的嵌入

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../template/foot.jsp"%>

在项目中应用freemarker就是这么简单,赶快用吧。


本文链接:FreeMarker在领悟教程网实战应用,本文由huangyineng原创,转载请注明出处


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics