失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 网站首页html静态化 网站首页怎么静态化

网站首页html静态化 网站首页怎么静态化

时间:2023-04-19 23:43:36

相关推荐

网站首页html静态化 网站首页怎么静态化

网站首页如何静态化

网站是个投资类的门户, 投资潮。

首页内容较多,有四屏多吧,数据库调用也很多,虽然数据库层做了缓存,页面上也使用了oscache,但还是感觉慢,于是想到做静态化。

用quartz来定时抓取动态页面的内容,生成静态页面

首页顶部的登录,用ajax来加载

execute

public void execute() {

String url="/index.do";

String filePath=PropertyUtil.getProperty("dbcapital.root.path")+"index.html";

//创建文件

File file=new File(filePath);

try{

if(!file.exists()){

file.createNewFile();

}

}catch(IOException e){}

//生成文件

String content=UrlfileInfo.readUrlContent(url);

if(!StringUtil.isEmpty(content)){

DocUtil.writeFile(content,filePath);//写静态文件

}

}

public static String readUrlContent(String url){

StringBuffer sb=new StringBuffer();

try{

String strLine;

URL urlObj = new URL(url);

InputStream streamObj = urlObj.openStream();

InputStreamReader readerObj = new InputStreamReader(streamObj,"GBK");

BufferedReader buffObj = new BufferedReader(readerObj);

while((strLine = buffObj.readLine())!=null)

sb.append(strLine+"\r");

buffObj.close();

}catch(MalformedURLException e){

System.err.println("url error");

}catch(IOException e){

System.out.println("IO error");

}

return sb.toString();

}

public static boolean writeFile(String str,String filePath){

boolean result=true;

try{

Writer fw = new OutputStreamWriter(new FileOutputStream (filePath),"GBK");

fw.write(str);

fw.close();

}catch(IOException e){

result=false;

System.out.println("写文件失败");

}

return result;

}

ajax加载登录代码

正在载入,请稍后...

将login.jsp的状态加载到loginInfo里

theHREF = window.top.location.href.split('/');

hostHREF='';

for(i=3;i

hostHREF=hostHREF+'/'+theHREF[i];

}

setTimeout("fillData('/top_login.jsp?fromUrl="+hostHREF+"&t="+new Date()+"','loginInfo')",1000);

替换登录内容的js

function fillData(url,elementId){

http_request=false;

if(window.ActiveXObject){

try{

http_request=new ActiveXObject("MSXML2.XMLHttp");

}catch(e){

try{

http_request=new ActiveXObject("Microsoft.XMLHTTP");

}catch(e){}

}

}else if (window.XMLHttpRequest) { // Mozilla, Safari, ...

http_request = new XMLHttpRequest();

http_request.overrideMimeType('text/xml');

}

http_request.onreadystatechange = function() {

if (http_request.readyState == 4) {

if (http_request.status == 200) {

var str=http_request.responseText;

document.getElementById(elementId).innerHTML=str;

} else {

alert("服务器忙,请稍候再试!");

}

} else {

document.getElementById(elementId).innerHTML="载入中,请稍候...";

}

};

http_request.open('GET', url+"&t="+new Date(), true);

http_request.send(null);

}

如果觉得《网站首页html静态化 网站首页怎么静态化》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。