您的位置:首页 > 编程语言 > Java开发

java静态页面生成技术--循环输出列表

2010-10-27 11:22 549 查看
运行环境(Struts2.1.6+Hibernate3)

/**
* 生成页面
*/
@Action(value = "crebusin", results = { @Result(name = "crebusinok", location = "../../bkmark/d.jsp") })
public String busincre() throws Exception {
req = ServletActionContext.getRequest();
req.setCharacterEncoding("UTF-8");

String fpath = req.getRealPath("/");// 获得服务器绝对路径

String templateContent = null;

FileInputStream fileinputstream = new FileInputStream(fpath
+ "mbtest.html");// 读取模板页,位于网站根目录
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);

session.beginTransaction();

Query qy = session.createQuery("from BusinBean");
/*
* qy.setFirstResult(0); qy.setMaxResults(4);
*/
businlist = qy.list();

for (Iterator iterator = businlist.iterator(); iterator.hasNext();) {
BusinBean b = (BusinBean) iterator.next();
templateContent = templateContent.replaceAll("#a" + x + "#", b.getBustitle());
x++;
}

FileOutputStream fileoutputstream = new FileOutputStream(fpath
+ "ok.html");
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();

session.getTransaction().commit();
return "crebusinok";
}


关键点:

for (Iterator iterator = businlist.iterator(); iterator.hasNext();) {
BusinBean b = (BusinBean) iterator.next();
templateContent = templateContent.replaceAll("#a" + x + "#", b.getBustitle());
x++;
}

模板页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="800" border="1" cellspacing="0" cellpadding="0">
<tr><td>#a1#</td></tr>
<tr><td>#a2#</td></tr>
<tr><td>#a3#</td></tr>
<tr><td>#a4#</td></tr>
<tr><td>#a5#</td></tr>
<tr><td>#a6#</td></tr>
<tr><td>#a7#</td></tr>
<tr><td>#a8#</td></tr>

</table>
</body>
</html>


使用变量X对应输出的模板页面,使用循环输出到指定位置

当前bug:输出中文乱码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: