在师父的讲解下,搞定了。本文章只用于分享学习经验,没有其他用意。希望大家培养开源的精神。
可能有有些一露的地方,大家看下前两篇文章。
可能有有些一露的地方,大家看下前两篇文章。
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserDao dao=new DaoImp();
List<Log4j> all=new ArrayList();
int pageCount = 4;//设置页数
int pages=dao.getPages(pageCount);
String webpagestr=createWebPage(pages);
for(int i=0;i<pages;i++){
all=dao.all(pageCount*i-i, pageCount);
creatHtml(request, all, i,webpagestr);
}
request.setAttribute("list", all);
return mapping.findForward("suce");
}
private String createWebPage(int pages) {
//设定连接页
StringBuilder webpagestr = new StringBuilder();
final String pagelink="<a href=";
for(int n=0;n<pages;n++){
//拼接连接
webpagestr.append(pagelink).append("log").append(n).append(".html>")
.append(n).append("</a>")
.append(" ");
}
String sss= webpagestr.toString();
return sss;
}
protected void creatHtml(HttpServletRequest request, List<Log4j> all,int pages,String webpagestr)
throws FileNotFoundException, IOException {
//------生成HTML-------------------------------
String filePath = "";
filePath = request.getRealPath("/")+"template.html";
/******** begin ************/
final String beginForHtmlTag = "<###for###>";
final String endForHtmlTag = "</###for###>";
FileInputStream input=new FileInputStream(filePath);
int length=input.available();
byte[] b=new byte[length];
input.read(b); //读取模板
String result = new String(b);//获取内容
int beginIndex = result.indexOf(beginForHtmlTag);
int endIndex = result.indexOf(endForHtmlTag);
String begin = result.substring(0, beginIndex);
String end = result.substring(endIndex+endForHtmlTag.length());
String table=result.substring(beginIndex+beginForHtmlTag.length(), endIndex);//get table
/**
* 替换所有
*/
StringBuilder content=new StringBuilder();
for(Log4j log4j:all){
String temp = table.replace("###username###",log4j.getUsername());
temp = temp.replace("###classs###",log4j.getClasss());
temp = temp.replace("###method###",log4j.getMethod());
temp = temp.replace("###leve###",log4j.getLogLevel());
temp = temp.replace("###mess###",log4j.getMessage());
temp = temp.replace("###titme###",log4j.getCreateTime());
content.append(temp);
}
result=begin+content+"<br />current pages:"+pages+"__"+webpagestr+end;
/******** end ************/
// 根据时间得文件名
// Calendar calendar = Calendar.getInstance();
// String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
String fileame ="log"+pages+".html";
//生成html路径
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
//写入
FileOutputStream fileoutputstream = new FileOutputStream(fileame);
byte tag_bytes[] = result.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
//---------------------------
}
}