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>