log4j插入数据库,并生成HTML(二)

后面是关键的东西也就是需要把查询的LIST.JSP页面生成HTML 了。
需要用的是一个IO流写入。
显示数据:

public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserDao dao=new DaoImp();
int pageCount = 4;//设置页数
// pages
int pages = dao.getPages(pageCount);
List<Log4j> all=new ArrayList();

for(int i =0;i<pages;i++){
all=dao.listall(pageCount,pageCount*i-i,pageCount);
creatHtml(request, all, i);
}

request.setAttribute("list", all);

return mapping.findForward("suce");
}



下面我们来看下生成HTML的方法:

protected void creatHtml(HttpServletRequest request, List<Log4j> all,int pages)
throws FileNotFoundException, IOException {
//------生成HTML-------------------------------
//截取模板,替换
final String beginForHtmlTag = "<###for###>";
final String endForHtmlTag = "</###for###>";
String filePath = "";
filePath = request.getRealPath("/")+"template.html";//取到模板
/******** begin ************/
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);

// String content = new String("");
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 += temp;
content.append(temp);
}

result = begin + content+"<br />all pages:"+pages + end;
/******** end ************/
以上是替换。这里会有事先上下页的功能,后期会加进去,大家期待。

// 根据时间得文件名
Calendar calendar = Calendar.getInstance();
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";

// String fileame ="a.html";

fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流


byte tag_bytes[] = result.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}


至于 HTML的模板 和网上很多资料写的一样。
大家可以查看下,如果有需要留言给我。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值