【笔记】FreeMarker模版引擎与struts2整合,实现【增删改查】生成静态页面

FreeMarker与struts2整合,一个小案例实现【增删改查】生成静态页面。内含数据库sql文件及效果图。数据库是mysql,c3p0+dbUtils实现数据操作。是一个完整的案例。支持源码下载哦!

【生成静态页面的类】

package com.athl.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class CreateHtml {

    /**
     * 
     * @param ftl       模版名.ftl
     * @param htmlName  静态页面的名字
     * @param map       数据
     * @throws IOException
     * @throws TemplateException
     */

    public void init(String ftl, String htmlName, Map map) throws IOException, TemplateException {

//一、创建负责管理 FreeMarker模板的 Configuration对象
          Configuration cfg = new Configuration();
          //指定FreeMarker模板文件的位置
          cfg.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "/WEB-INF/template/");
          cfg.setEncoding(Locale.getDefault(), "utf-8");

//二、创建Template对象
          Template template = cfg.getTemplate(ftl);
          template.setEncoding("utf-8");

//三、生成静态页面
          //设置文件类型
          htmlName=htmlName+".html";
          //设置页面保存位置
          String path = ServletActionContext.getServletContext().getRealPath("/");
          File fileName = new File(path + htmlName);
          Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"));
          //将数据与模版整合,生成静态页面
          template.process(map, out);
          out.flush();
          out.close();
    }

}

【Action】

package com.athl.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.athl.bean.Person;
import com.athl.dao.PersonDao;
import com.athl.utils.CreateHtml;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import freemarker.template.TemplateException;

public class PersonAction extends ActionSupport implements ModelDriven<Person> {

    private static final long serialVersionUID = -814210299977683837L;
    private String url,msg; 
    private Person p;

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getMsg() {
      return msg;
    }
    public void setMsg(String msg) {
      this.msg = msg;
    }
    public Person getModel() {
        if(p==null){
            p= new Person();
        }
        return p;
    }

    public String getAll() throws Exception {
        CreateHtml createHtml = new CreateHtml();
        Map<String, List<Person>> map = new HashMap<String, List<Person>>();
        map.put("personlist", new PersonDao().query());

        String htmlName = "personList";
        String ftl = "list.ftl";

        try {
          createHtml.init(ftl, htmlName, map);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (TemplateException e) {
          e.printStackTrace();
        }
        url = "/"+htmlName;
        return "list";
    }

    public String addPage() throws Exception {
        CreateHtml createHtml = new CreateHtml();
        //Map<String, List<Person>> map = new HashMap<String, List<Person>>();
        //map.put("personlist", new PersonDao().query());
        String htmlName = "add";
        String ftl = "add.ftl";

        try {
          createHtml.init(ftl, htmlName, null);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (TemplateException e) {
          e.printStackTrace();
        }
        url = "/"+htmlName;
        return "addPage";
    }

    public String add() throws Exception {
        new PersonDao().add(p);
        return getAll();
    }

    public String queryById() throws Exception {
        CreateHtml createHtml = new CreateHtml();
        Map<String, Person> map = new HashMap<String,Person>();
        map.put("person", new PersonDao().queryById(p.getId()));
        String htmlName = "update";
        String ftl = "update.ftl";

        try {
          createHtml.init(ftl, htmlName, map);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (TemplateException e) {
          e.printStackTrace();
        }
        url = "/"+htmlName;
        return "update";
    }
    public String update() throws Exception {
        new PersonDao().update(p);
        return getAll();
    }

    public String delete() throws Exception {
        new PersonDao().del(p.getId());
        return getAll();
    }

    public String queryLike() throws Exception {
        CreateHtml createHtml = new CreateHtml();
        Map<String, List<Person>> map = new HashMap<String, List<Person>>();
        map.put("personlist", new PersonDao().queryLike(msg));
        String htmlName = "personListByLike";
        String ftl = "list.ftl";

        try {
          createHtml.init(ftl, htmlName, map);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (TemplateException e) {
          e.printStackTrace();
        }
        url = "/"+htmlName;
        return "list";
    }
}

【struts .xml】

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 开发模式开启(经测试不开启也能正常运行) -->
    <!-- <constant name="struts.devMode" value="true" /> -->
    <package name="person" namespace="/" extends="struts-default">
        <action name="*" class="com.athl.action.PersonAction" method="{1}">
            <result name="list" type="redirect">${url }.html</result>
            <result name="update" type="redirect">${url }.html</result>
            <result name="addPage" type="redirect">${url }.html</result>
        </action>
    </package>
</struts>

源码下载:http://download.csdn.net/detail/jul_11th/9731594

谢谢支持!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值