java静态页面生成技术-批量数据生成
2010-10-31 11:37
204 查看
前言:一直都在寻找java模式下静态页面的生成方法,终于上到了新的台阶了,批量数据生成初步实现,呵呵
前置条件:运行环境(Struts2.16以上+Hibernate3.5以上,这是我的运行环境,未必按照这个,根据实际需求定制)
实现原理:使用Struts2的action运行两次数据查询存入结果集,同时替换指定模板页中的对应字符,生成静态页面,实现静态生成
具体看代码
1.核心代码(使用注解模式操作):
2.模板页代码:
3.使用方法:
http://localhost:8080/项目名称/cretp
后记:
当前还有些bug,但不影响运行
bug:字符转码问题,模板页必须设为GBK编码,UTF-8会出现中文乱码
前置条件:运行环境(Struts2.16以上+Hibernate3.5以上,这是我的运行环境,未必按照这个,根据实际需求定制)
实现原理:使用Struts2的action运行两次数据查询存入结果集,同时替换指定模板页中的对应字符,生成静态页面,实现静态生成
具体看代码
1.核心代码(使用注解模式操作):
package com.marry.action; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import org.hibernate.Query; import org.hibernate.Session; import com.opensymphony.xwork2.ActionSupport; import com.marry.model.*; import com.marry.tools.HibernateUtil; public class GetAllListAction extends ActionSupport { private HttpServletRequest req; private NewsBean nwBean; private YhmsgBean yhmBean; private Query qy; private int x = 1; public List<NewsBean> nwlist = new ArrayList<NewsBean>(); public List<YhmsgBean> yhmlist = new ArrayList<YhmsgBean>(); private Session session = HibernateUtil.getSessionFactory() .getCurrentSession(); public NewsBean getNwBean() { return nwBean; } public void setNwBean(NewsBean nwBean) { this.nwBean = nwBean; } public YhmsgBean getYhmBean() { return yhmBean; } public void setYhmBean(YhmsgBean yhmBean) { this.yhmBean = yhmBean; } @Action(value = "cretp", results = { @Result(name = "getok", location = "2.jsp") }) public String doList() throws Exception { req = ServletActionContext.getRequest(); String fpath = req.getRealPath("/");//获得服务器绝对路径 String templateContent = null; /*读取模板文件*/ FileInputStream fileinputstream = new FileInputStream(fpath + "p.html"); int lenght = fileinputstream.available(); byte bytes[] = new byte[lenght]; StringBuffer sb = new StringBuffer(); /*字符转码,防止出现乱码*/ while (fileinputstream.read(bytes) != -1) { sb.append(new String(bytes, "GBK")); } fileinputstream.close(); templateContent = new String(bytes); /*准备数据库读取操作*/ session.beginTransaction(); /*第一个结果集读取*/ qy = session.createQuery("from NewsBean"); nwlist = qy.list(); /*循环结果集,将指定字段替换模板中对应的位置*/ for (Iterator<NewsBean> iterator = nwlist.iterator(); iterator .hasNext();) { NewsBean nwb = iterator.next(); templateContent = templateContent.replaceAll("##d1" + x + "#", nwb .getNwid() + ""); templateContent = templateContent.replaceAll("##t1" + x + "#", nwb .getNwtitle()); x++; } x=1;//还原初始,否则下一个输出会从当前x的值开始取值 /*第二个结果集查询开始*/ qy = session.createQuery("from YhmsgBean"); yhmlist = qy.list(); for (Iterator<YhmsgBean> iterator = yhmlist.iterator(); iterator .hasNext();) { YhmsgBean yhm = iterator.next(); templateContent = templateContent.replaceAll("##d2" + x + "#", yhm .getYhmsgid() + ""); templateContent = templateContent.replaceAll("##t2" + x + "#", yhm .getYhmsgtitle()); x++; } /* 输出静态页面 */ FileOutputStream fileoutputstream = new FileOutputStream(fpath + "sp.html"); byte tag_bytes[] = templateContent.getBytes(); fileoutputstream.write(tag_bytes); fileoutputstream.close(); session.getTransaction().commit(); return "getok"; } }
2.模板页代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>无标题文</title> </head> <body> <table width="200px" border="1"> <tr> <td> A表ID </td> <td> A表标题 </td> </tr> <tr> <td> ##d11# </td> <td> ##t11# </td> </tr> <tr> <td> ##d12# </td> <td> ##t12# </td> </tr> <tr> <td> ##d13# </td> <td> ##t13# </td> </tr> <tr> <td> ##d14# </td> <td> ##t14# </td> </tr> <tr> <td> ##d15# </td> <td> ##t15# </td> </tr> </table> <table width="200px" border="1"> <tr> <td> B表ID </td> <td> B表标题 </td> </tr> <tr> <td> ##d21# </td> <td> ##t21# </td> </tr> <tr> <td> ##d22# </td> <td> ##t22# </td> </tr> <tr> <td> ##d23# </td> <td> ##t23# </td> </tr> <tr> <td> ##d24# </td> <td> ##t24# </td> </tr> <tr> <td> ##d25# </td> <td> ##t25# </td> </tr> </table> </body> </html>
3.使用方法:
http://localhost:8080/项目名称/cretp
后记:
当前还有些bug,但不影响运行
bug:字符转码问题,模板页必须设为GBK编码,UTF-8会出现中文乱码
相关文章推荐
- java静态页面生成技术--前期测试阶段2
- java静态页面生成技术--Servlet测试
- java静态页面生成技术--循环输出列表
- java静态页面生成技术--循环输出列表--BUG临时修复
- 用JAVA生成网站静态页面技术解决方案
- JAVA使用jsoup技术实现网站URL解析爬取|爬取网站登陆后页面动态数据
- 利用Java生成静态HMTL页面的方法收集
- 利用PHP与Smarty模板技术生成分页静态页面的代码
- 【转帖】利用Java生成静态HTML页面
- 利用Java生成静态HMTL页面的方法收集
- Java生成静态HTML页面
- 利用Java生成静态HMTL页面
- Java高并发:静态页面生成方案
- java 生成静态页面——Freemarker实例教程
- java 生成静态页面——Freemarker实例教程
- Java高并发:静态页面生成方案
- 利用Java生成静态HMTL页面的方法
- 利用Java生成静态HMTL页面(转贴)
- ASP网站生成静态html页面技术小结
- 利用Java生成静态HMTL页面