Spring MVC代码实例系列-09:Spring MVC配置Freemarker实现页面静态化的简单实例

超级通道 :Spring MVC代码实例系列-绪论

本章主要记录Spring MVC配置Freemarker实现页面静态化的简单实例,涉及到的技术有:

  • @PathVariable通过获取URL上以模板{}标记的参数。
  • @ModelAttribute 设置Model里的属性,本例中用来模拟数据库。
  • FreeMarkerConfigurer 获取freemarker模板的配置bean
  • FreeMarker模板

##1.程序目录

src
\---main
    \---java
    |   \---pers
    |       \---hanchao
    |           \---hespringmvc
    |               \---freemarker
    |                   \---News.java
    |                   \---FreeMarkerController.java
    |                   \---NewsController.java
    \---webapp
        \---freemarker
        |   \---index.jsp
        |   \---main.ftl
        |   \---news.ftl
        \---html
        |   \---news
        \---WEB-INF
        |   \---spring-mvc-servlet.xml
        |   \---web.xml
        \---index.jsp

##2.pom.xml

    <freemarker.version>2.3.23</freemarker.version>
    
    <!--freemarker-->
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>${freemarker.version}</version>
    </dependency>

##3.spring-mvc-servlet.xml

    <!--freemarker-->
    <!-- 配置freeMarker视图解析器 并没有用到 -->
    <!--<bean id="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">-->
        <!--<property name="viewClass" value="pers.hanchao.hespringmvc.freemarker.FreeMarkerViewUtil"/>-->
        <!--<property name="contentType" value="text/html; charset=UTF-8"/>-->
        <!--<property name="exposeRequestAttributes" value="true" />-->
        <!--<property name="exposeSessionAttributes" value="true" />-->
        <!--<property name="exposeSpringMacroHelpers" value="true" />-->
        <!--<property name="cache" value="true" />-->
        <!--<property name="prefix" value="/"/>-->
        <!--<property name="suffix" value=".ftl" />-->
        <!--<property name="order" value="2"/>-->
    <!--</bean>-->

    <!-- 配置freeMarker的模板路径 -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/freemarker/"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">3600</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="number_format">#.##</prop>
            </props>
        </property>
    </bean>

##4.News.java

package pers.hanchao.hespringmvc.freemarker;

/**
 * <p>新闻</p>
 * @author hanchao 2018/1/21 13:31
 **/
public class News {
    /** 新闻编号 */
    private Integer id;
    /** 新闻标题 */
    private String title;
    /** 新闻内容 */
    private String content;

    public News() {}

    public News(Integer id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }
    //toString
    //settr and getter
}

##5.FreeMarkerController.java

package pers.hanchao.hespringmvc.freemarker;

import freemarker.template.Template;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import pers.hanchao.hespringmvc.log4j.App;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Map;

/**
 * <p>提供创建HTML文件的方法的父类</p>
 * @author hanchao 2018/1/21 18:12
 **/
public class FreeMarkerController {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    private String charset = "UTF-8";
    private static final Logger LOGGER = Logger.getLogger(App.class);
    /** 过期时间单位 毫秒*/
    private Long expireTime = 86400000L;

    /**
     * <p></p>
     * ftlFileName 模板文件名
     * model 数据模型
     * htmlFilePath 保存的html文件全路径
     * @author hanchao 2018/1/21 16:19
     **/
    public String creatHtml(HttpServletRequest request, String ftlFileName, Map model, String htmlMappingPath) throws Exception{

        //获取目标Html文件的绝对路径
        String htmlFilePath = request.getServletContext().getRealPath("/") + htmlMappingPath.replace("/",File.separator) + ".html";
        LOGGER.debug("htmlFilePath:" + htmlFilePath);

        //如果文件不存在 或者 文件存在且已经超过了过期时间,则重新生成
        //或者说如果文件存在且还未超过过期时间,则什么也不做
        File file = new File(htmlFilePath);
        if (file.exists()){
            Long interval = System.currentTimeMillis() - file.lastModified() - this.expireTime;
            LOGGER.debug("System.currentTimeMillis():" + System.currentTimeMillis());
            LOGGER.debug("file.lastModified():" +  file.lastModified());
            LOGGER.debug("this.expireTime:" +  this.expireTime);
            LOGGER.debug("interval:" + interval);
            if (interval < 0){
                return htmlMappingPath;
            }else {//如果文件超过了过期时间,则删除文件
                file.delete();
            }
        }

        //读取模板文件,填充模型数据,形成html字符串
        StringWriter sw = new StringWriter();
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(ftlFileName + ".ftl");
        template.process(model,sw);
        sw.close();
        String htmlStr =  sw.toString();

        //将html字符串写到html文件中
        file.createNewFile();
        OutputStream outputStream = new FileOutputStream(file,true);
        outputStream.write(htmlStr.getBytes(charset));
        outputStream.close();
        LOGGER.info("生成HTML文件:" + htmlFilePath);
        return htmlMappingPath;
    }
}

##6.NewsController.java

package pers.hanchao.hespringmvc.freemarker;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>freemarker页面的静态化的简单实例</p>
 * @author hanchao 2018/1/21 18:12
 **/
@Controller
@RequestMapping("freemarker")
public class NewsController extends FreeMarkerController {

    /**
     * <p>通过@ModelAttribute造数</p>
     * @author hanchao 2018/1/21 13:40
     **/
    @ModelAttribute
    public void init(Model model){
        News[] newsArray = new News[10];
        Integer id = 1000000;
        String title = "新闻标题00";
        String content = "新闻内容00";
        for (int i = 0; i < 10; i++) {
            newsArray[i] = new News(id + i,title + i,content + i);
        }
        model.addAttribute("newsArray",newsArray);
    }
    /**
     * <p>获取新闻列表</p>
     * @author hanchao 2018/1/21 13:35
     **/
    @GetMapping("/news/")
    public String getAllNews(HttpServletRequest request,@ModelAttribute("newsArray") News[] newsArray, Model model) throws Exception {
        Map newsMap = new HashMap<String ,News[]>();
        newsMap.put("newsArray",newsArray);

        return this.creatHtml(request,"main",newsMap,"/html/news/main");
    }

    /**
     * <p>获取新闻详情</p>
     * @author hanchao 2018/1/21 13:49
     **/
    @GetMapping("/news/{id}")
    public String getNews(HttpServletRequest request,@PathVariable Integer id,@ModelAttribute("newsArray") News[] newsArray, Model model) throws Exception {
        Map newsMap = new HashMap<String ,News[]>();
        newsMap.put("news",newsArray[id - 1000000]);

        return this.creatHtml(request,"news",newsMap,"/html/news/" + id);
    }

    /**
     * <p></p>
     * @author hanchao 2018/1/21 18:17
     **/
    @GetMapping("/ftl")
    public String getFtl(Model model){
        model.addAttribute("title","ftl测试");
        model.addAttribute("content","这是一个ftl测试");
        model.addAttribute("CREATE_HTML", true);
        return "/freemarker/demo";
    }

}

##7.main.ftl

<html>
<head>
    <title>新闻首页</title>
</head>
<body>
    <#list newsArray as news>
    <li>
        <a href="/freemarker/news/${news.id}">${news.title}</a>
    </li>
    </#list>
</body>
</html>

##8.news.ftl

<html>
<head>
    <title>新闻详情</title>
</head>
<body>
    <h3>${news.title}</h3>
    <p>${news.title}</p>
</body>
</html>

##9.result
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值