这样的项目我做过,思想是这样的,先用Dreamwaver等软件做几个网页的模板出来,一个网页通过是包括上下左右四个公共部分的,例如,上面显示banner,左边显示功能菜单,右边显示广告条,下面显示copyright等等,这四个部分做成include文件,中间留空来生成动态的内容。然后分别把这四个include部分的HTML代码存入数据库中。到时再写后台管理页面,用流文件的方式来生成静态页,过程就是生成先前做好的那个页面的上下左右四个部分,然后从数据库中读出动态动分,填充中间的部分,这其实都不难的,难点其实就是页面的问题,有时生成的页面上的部分位置会有不对,要多次调整的。
但是还有一个疑问,那就是从数据库中读出数据之后如何才能写入html文件,生成能够直接访问的
html的文件呢?请大虾指教!
当然,完全动态的页面也可以velocity来做,据我的测试,velocity生成页面的效率要比jsp略高,但考虑到数据库访问的瓶颈,可以认为效率相等。
我做的这个模块不需要分页,一页全部读出, 考虑到某个时间短内访问量会很大,如果总是读取数据库,数据库压力就会很大,虽然用了
hibernate 所以考虑生成静态的html。 另外在使用spring+hibernate频繁插入数据的的时候,如何来提高效率!?
<%
try{
String title="This is Title";
String content="This is Content Area";
String editer="LaoMao";
String filePath = "";
filePath = request.getRealPath("/")+"test/template.htm";
//out.print(filePath+"<br/>");
String templateContent="";
FileInputStream fileinputstream = new FileInputStream(filePath);//读取模块文件
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
templateContent = new String(bytes);
//out.print(templateContent);
templateContent=templateContent.replaceAll("###title###",title);
templateContent=templateContent.replaceAll("###content###",content);
templateContent=templateContent.replaceAll("###author###",editer);//替换掉模块中相应的地方
//out.print(templateContent);
// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
catch(Exception e){
out.print(e.toString());
}%>2. template.htm<html>
<head>
<title>###title###</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="../css.css" rel=stylesheet type=text/css>
</head><body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="2">
<tr>
<td align="center">###title###</td>
</tr>
<tr>
<td align="center">author:###author### </td>
</tr>
<tr>
<td>###content###
</td>
</tr></table>
</body>
</html>
你这还是动态的...原理倒和velocity差不多
String top_part="XXX"(XXX即数据库中读出的内容);String left_part="XXX";String right_part="XXX";String bottom_part="XXX";String middle_part="XXX";String all_part=top_part+left_part+.......+middle_part;然后用FileOutPutStream.write(all_part);来生成一个静态页
楼上好主意:)说说用struts的时候,如何使用缓存吧
=================
那天跟踪代码,发现在hibernate有个 org.hibernate.cache包,
你可以去看看,借鉴一下,有收获记得分享哦。
writeString.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n"); writeString.append("<HTML><HEAD><meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\"><TITLE>用户调查问卷</TITLE>\r\n");
writeString.append("</head>\r\n");
writeString.append("<BODY topMargin=0>\r\n");
writeString.append("<form name=\"form1\" method=\"post\" action=\"\">");
writeString.append("<table width=\"29%\" height=\"119\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">\r\n");
writeString.append("<tr><td>"+surveyInfo.getSurveyName()+"</td></tr>\r\n");
for(int i=0;i<qusNum;i++){
QuestionInfo question = questions[i];
AppPollOption[] option = question.getPollOptions();
writeString.append("<tr><td>");
writeString.append(question.getQuestion());
writeString.append("</tr></td>\r\n");
for(int j=0;j<option.length;j++){
writeString.append("<tr><td>");
if(question.getMultiChoice().intValue()==0){
writeString.append("");
}else{
}
writeString.append("</tr></td>\r\n");
}
}
writeString.append("</table>\r\n");
writeString.append("</form>");
writeString.append("</BODY></HTML>\r\n");
} if (!writeFile(filePath, writeString)) {
throw new Exception("向文件中写数据出错!");
} }
页头加上
<%@ taglib uri="/WEB-INF/classes/oscache.tld" prefix="cache" %>再用以下标签,把要缓存的逻辑代码跟html代码括起来<cache:cache key="product_list" groups="product" time="3600" scope="application"></cache:cache>这样就行了。scope是缓存的范围,这个跟javabean一样,time是缓存的有效时间,以秒为单位,最好自己去查一下文档吧。