JAVA静态化,如何生成html问题

harryzyp 2008-08-29 02:57:58
小弟在网上找过很多方案,大多是IO流生成文件,感觉方案不怎么好

大家有没有什么好的思路?在这分享一下,呵呵
...全文
2411 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingork 2008-09-13
  • 打赏
  • 举报
回复
大型网站的信息系统都得做动态页面的静态化,心减少服务器的压力,

可不可以先把动态页面暂存起来,,,然后在每天固定服务器空闲的时候再专门做静态化的工作啊??

高手指正。

  • 打赏
  • 举报
回复
路过,过来学习一下。
wangyi123 2008-09-02
  • 打赏
  • 举报
回复
偶不懂,过来弱弱的问一声,问什么需要静态化的页面呢?这样有什么好处呢?
nick6610 2008-09-01
  • 打赏
  • 举报
回复
freemarker
小雨转晴 2008-09-01
  • 打赏
  • 举报
回复
我是情况和lz一样,路过学习。
友情帮顶,高手都过来哦!o(∩_∩)o...
harryzyp 2008-09-01
  • 打赏
  • 举报
回复
楼上兄弟,你这代码真够长,不过很谢谢你...
rock_LL 2008-08-29
  • 打赏
  • 举报
回复
public class ConfigResolver {
private static String xmlPath = "config/config.xml";

/**
* 获取配置文件对象信息
*
* @param fileName
* @return
* @throws Exception
*/
public static Config parseXML(String fileName) {
if (null != fileName) {
xmlPath = fileName;
}
Config configFile = new Config();
try {
XMLConfiguration parser = new XMLConfiguration();
parser.setFile(new File(xmlPath));
parser.load();

//首页原
String indexFrom = (String) parser.getProperty("indexFrom");
configFile.setIndexFrom(indexFrom);
//首页目标
String indexTo = (String) parser.getProperty("indexTo");
configFile.setIndexTo(indexTo);

//圈子页原
String groupZhengquanFrom = (String) parser
.getProperty("groupZhengquanFrom");
configFile.setGroupZhengquanFrom(groupZhengquanFrom);
//圈子页目标
String groupZhengquanTo = (String) parser
.getProperty("groupZhengquanTo");
configFile.setGroupZhengquanTo(groupZhengquanTo);

//频道页原
String channelFrom = (String) parser.getProperty("channelFrom");
configFile.setChannelFrom(channelFrom);
//频道页目标
String channelTo = (String) parser.getProperty("channelTo");
configFile.setChannelTo(channelTo);

String channelIds = (String) parser.getProperty("channelIds");
configFile.setChannelIds(channelIds);

//大杂烩原
String dazahuiFrom = (String) parser.getProperty("dazahuiFrom");
configFile.setDazahuiFrom(dazahuiFrom);
//大杂烩目标
String dazahuiTo = (String) parser.getProperty("dazahuiTo");
configFile.setDazahuiTo(dazahuiTo);

//别人的订阅,关注原
String anthorTakenFrom = (String) parser
.getProperty("anthorTakenFrom");
configFile.setAnthorTakenFrom(anthorTakenFrom);
//别人的订阅,关注目标
String anthorTakenTo = (String) parser.getProperty("anthorTakenTo");
configFile.setAnthorTakenTo(anthorTakenTo);

//房产圈子页原
String fangChanFrom = (String) parser
.getProperty("groupFangchanFrom");
configFile.setGroupFangchanFrom(fangChanFrom);
//房产圈子页目标
String fangChanTo = (String) parser.getProperty("groupFangchanTo");
configFile.setGroupFangchanTo(fangChanTo);
String groupIds = (String) parser.getProperty("groupIds");
configFile.setGroupIds(groupIds);

//首页用圈子文章list原
String quanziArticleListFrom = (String) parser.getProperty("quanziArticleListFrom");
configFile.setQuanziArticleListFrom(quanziArticleListFrom);
//首页用圈子文章list目标
String quanziArticleListTo = (String) parser.getProperty("quanziArticleListTo");
configFile.setQuanziArticleListTo(quanziArticleListTo);

String quanziIds = (String) parser.getProperty("quanziIds");
configFile.setQuanziIds(quanziIds);


//圈子首页原
String groupIndexFrom = (String) parser.getProperty("groupIndexFrom");
configFile.setGroupIndexFrom(groupIndexFrom);
//圈子首页目标
String groupIndexTo = (String) parser.getProperty("groupIndexTo");
configFile.setGroupIndexTo(groupIndexTo);

//社区首页原
String shequIndexFrom = (String) parser.getProperty("shequIndexFrom");
configFile.setShequIndexFrom(shequIndexFrom);
//社区首页目标
String shequIndexTo = (String) parser.getProperty("shequIndexTo");
configFile.setShequIndexTo(shequIndexTo);

//热点排名周源
String articleRankWeekFrom = (String) parser.getProperty("articleRankWeekFrom");
configFile.setArticleRankWeekFrom(articleRankWeekFrom);
//热点排名周目标
String articleRankWeekTo = (String) parser.getProperty("articleRankWeekTo");
configFile.setArticleRankWeekTo(articleRankWeekTo);

//热点排名月源
String articleRankMonthFrom = (String) parser.getProperty("articleRankMonthFrom");
configFile.setArticleRankMonthFrom(articleRankMonthFrom);
//热点排名月目标
String articleRankMonthTo = (String) parser.getProperty("articleRankMonthTo");
configFile.setArticleRankMonthTo(articleRankMonthTo);

//图片首页more源
String photoIndexMoreFrom = (String) parser.getProperty("photoIndexMoreFrom");
configFile.setPhotoIndexMoreFrom(photoIndexMoreFrom);
//图片首页more目标
String photoIndexMoreTo = (String) parser.getProperty("photoIndexMoreTo");
configFile.setPhotoIndexMoreTo(photoIndexMoreTo);

//博客分享源
String blogerShareFrom = (String) parser.getProperty("blogerShareFrom");
configFile.setBlogerShareFrom(blogerShareFrom);
//博客分享目标
String blogerShareTo = (String) parser.getProperty("blogerShareTo");
configFile.setBlogerShareTo(blogerShareTo);

} catch (Exception e) {
e.printStackTrace();
}
return configFile;
}
}
rock_LL 2008-08-29
  • 打赏
  • 举报
回复
public class Test {

//config.getDriverName(),config.getUrl(),config.getUserName(),config.getPassword());
public static void main(String[] args) {
Config config = ConfigResolver.parseXML(null);

String jspPage = config.getIndexFrom();//jsp文件地址
String htmlPage = config.getIndexTo();//html文件名
if(jspPage!=null && htmlPage!=null ){
createHtml(jspPage,htmlPage); //处理站点首页
}

jspPage = config.getGroupZhengquanFrom();//jsp文件地址
htmlPage = config.getGroupZhengquanTo();//html文件名
if(jspPage!=null && htmlPage!=null ){
createHtml(jspPage,htmlPage); //处理证券圈子页面
}
public final static void copyFile(BufferedReader from, BufferedWriter to) {
if (to == null || from == null)
return;//不生成静态页面
try {
int c;
while ((c = from.read()) != -1) {
to.write((char)c);
}
to.flush();
close(from, to);
} catch (IOException e) {
e.printStackTrace();
}
}

public final static BufferedReader getIn(String jspPage) {

BufferedReader in = null;
URL url = null;
URLConnection urlConn = null;
try {
url = new URL(jspPage);
urlConn = url.openConnection();//建立http连接
in = new BufferedReader(new InputStreamReader(urlConn
.getInputStream(), "GBK"));//设置Encoding,必须设置,否则显示中文会有问题
} catch (IOException e) {
e.printStackTrace();

}finally{
urlConn=null;
url=null;
}


return in;
}

public final static BufferedWriter getOut(String htmlPage, boolean flag) {

BufferedWriter out = null;

try {
File htmlFile = new File(htmlPage);
if (flag) {
htmlFile.createNewFile();
} else {//如果flag为false则不覆盖文件
if (htmlFile.exists()) {//如果文件已经存在则返回null
return null;
} else {//建立新文件
htmlFile.createNewFile();
}
}
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile), "GBK"));//设置Encoding
} catch (IOException e) {
e.printStackTrace();
}

return out;

}//flag为真则覆盖原文件

public final static void close(BufferedReader in, BufferedWriter out) {

try {
in.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
rock_LL 2008-08-29
  • 打赏
  • 举报
回复
public class Test {

//config.getDriverName(),config.getUrl(),config.getUserName(),config.getPassword());
public static void main(String[] args) {
Config config = ConfigResolver.parseXML(null);

String jspPage = config.getIndexFrom();//jsp文件地址
String htmlPage = config.getIndexTo();//html文件名
if(jspPage!=null && htmlPage!=null ){
createHtml(jspPage,htmlPage); //处理站点首页
}

jspPage = config.getGroupZhengquanFrom();//jsp文件地址
htmlPage = config.getGroupZhengquanTo();//html文件名
if(jspPage!=null && htmlPage!=null ){
createHtml(jspPage,htmlPage); //处理证券圈子页面
}
public static void createHtml(String jspPage,String htmlPage){
System.out.println(jspPage+"----------------start");
BufferedReader in = null;
BufferedWriter out= null;
try{
in = new BufferedReader(new InputStreamReader(new FileInputStream(htmlPage)));
out = getOut(htmlPage+"_old", true); //备分原文件
copyFile(in,out); //备分原文件
}catch(Exception e){
e.printStackTrace();
}

in = getIn(jspPage);
out = getOut(htmlPage+"_back", true);
generateHtml(in, out,jspPage);//生成静态页面
close(in, out);

try{
in=new BufferedReader(new InputStreamReader(new FileInputStream(htmlPage+"_back")));
}catch(Exception e){
e.printStackTrace();
}
out = getOut(htmlPage, true);
copyFile(in,out); //文件拷贝
System.out.println(jspPage+"----------------end");
}

public final static void generateHtml(BufferedReader in, BufferedWriter out,String jspPage) {
boolean flag=true;

if (out == null)
return;//不生成静态页面
try {
out.write("<%@ page language=\"java\" %>\r\n");
out.write("<%@ page contentType=\"text/html; charset=GBK\"%>\r\n");
String c;
while ((c = in.readLine()) != null) {
// if(c.trim().equals("<!--portalheaderbegin-->")){
// flag=false;
// }
// if(c.trim().equals("<!--portalheaderend-->")){
// if(jspPage.indexOf("channel.jsp")!=-1){ //如果是频道页面
// c="<jsp:include page='/include/portalheader.jsp?channelId="+jspPage.split("type_id=")[1]+"' />\r\n";
// }else if(jspPage.indexOf("zhengquan_group_act.jsp")!=-1){ //证券圈
// c="<jsp:include page='/include/portalheader.jsp?groupId=43' />\r\n";
// }else if(jspPage.indexOf("group_general.jsp")!=-1){ //其他圈子
// c="<jsp:include page='/include/portalheader.jsp?groupId="+jspPage.split("type_id=")[1]+"' />\r\n";
// }else if(jspPage.indexOf("group_index")!=-1){ //圈子首页
// c="<jsp:include page='/include/portalheader.jsp?groupId=group' />\r\n";
// }else if(jspPage.indexOf("community_index")!=-1){ //社区首页
// c="<jsp:include page='/include/portalheader.jsp?shequId=abc123' />\r\n";
// }else{
// c="<jsp:include page='/include/portalheader.jsp' />\r\n";
// }
// flag=true;
// }

if(c.trim().equals("<!--portalheaderbegin-->")){
flag=false;
}

if(c.trim().equals("<!--portalheaderend-->")){
if(jspPage.indexOf("channel.jsp")!=-1){ //如果是频道页面
//c="<jsp:include page='/include/portalheader.jsp?channelId="+jspPage.split("type_id=")[1]+"' />\r\n";
c="<jsp:include page='/permissionFilter.jsp' />\r\n";
}else if(jspPage.indexOf("zhengquan_group_act.jsp")!=-1){ //证券圈
c="<jsp:include page='/include/portalheader.jsp?groupId=43' />\r\n";
}else if(jspPage.indexOf("group_general.jsp")!=-1){ //其他圈子
//c="<jsp:include page='/include/portalheader.jsp?groupId="+jspPage.split("type_id=")[1]+"' />\r\n";
c="<jsp:include page='/permissionFilter.jsp' />\r\n";
}else if(jspPage.indexOf("group_index")!=-1){ //圈子首页
c="<jsp:include page='/include/portalheader.jsp?groupId=group' />\r\n";
}else if(jspPage.indexOf("community_index")!=-1){ //社区首页
c="<jsp:include page='/include/portalheader.jsp?shequId=abc123' />\r\n";
}else if(jspPage.indexOf("portal/index_act.jsp")!=-1){ //首页
//c="<jsp:include page='/include/portalheader.jsp?index=yes' />\r\n";
c="<jsp:include page='/permissionFilter.jsp' />\r\n";
}else if(jspPage.indexOf("portal/blogershare_act.jsp")!=-1){ //分享页
c="<jsp:include page='/permissionFilter.jsp' />\r\n";
}else{
c="<jsp:include page='/include/portalheader.jsp' />\r\n";
}
flag=true;
}
if(flag&&c.length()>0){
out.write(c+"\r\n");
}
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public final static void copyFile(BufferedReader from, BufferedWriter to) {
if (to == null || from == null)
return;//不生成静态页面
try {
int c;
while ((c = from.read()) != -1) {
to.write((char)c);
}
to.flush();
close(from, to);
} catch (IOException e) {
e.printStackTrace();
}
}

public final static BufferedReader getIn(String jspPage) {

BufferedReader in = null;
URL url = null;
URLConnection urlConn = null;
try {
url = new URL(jspPage);
urlConn = url.openConnection();//建立http连接
in = new BufferedReader(new InputStreamReader(urlConn
.getInputStream(), "GBK"));//设置Encoding,必须设置,否则显示中文会有问题
} catch (IOException e) {
e.printStackTrace();

}finally{
urlConn=null;
url=null;
}


return in;
}

public final static BufferedWriter getOut(String htmlPage, boolean flag) {

BufferedWriter out = null;

try {
File htmlFile = new File(htmlPage);
if (flag) {
htmlFile.createNewFile();
} else {//如果flag为false则不覆盖文件
if (htmlFile.exists()) {//如果文件已经存在则返回null
return null;
} else {//建立新文件
htmlFile.createNewFile();
}
}
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(htmlFile), "GBK"));//设置Encoding
} catch (IOException e) {
e.printStackTrace();
}

return out;

}//flag为真则覆盖原文件

public final static void close(BufferedReader in, BufferedWriter out) {

try {
in.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
harryzyp 2008-08-29
  • 打赏
  • 举报
回复
freemarker还没接触过...,我去查查看

[Quote=引用 5 楼 yuezu1026 的回复:]
我最近做了一个静态化的框架,网站的静态化率高达99%。
我负责的是www.lighting86.com.cn,包括分页的我都静态化了。不过静态的的页面数我进行了控制,目前是超过10页,自动转向动态页。
[/Quote]
楼上这个网站做的不错,静态化怎么实现的?说说思路啊
yuezu1026 2008-08-29
  • 打赏
  • 举报
回复
我最近做了一个静态化的框架,网站的静态化率高达99%。
我负责的是www.lighting86.com.cn,包括分页的我都静态化了。不过静态的的页面数我进行了控制,目前是超过10页,自动转向动态页。
kenshinlou 2008-08-29
  • 打赏
  • 举报
回复
freemarker 是很NB的,当然还是要生成html的,你不会想每次都解析然后输出吧,要明白比较固定的东西才有做静态化的需要.
twtetg 2008-08-29
  • 打赏
  • 举报
回复
新手路过,学习下。。。
zhkchi 2008-08-29
  • 打赏
  • 举报
回复
freemarker是不错的,做这个.楼上那个也可以.就是没好的工具支持
qsrock 2008-08-29
  • 打赏
  • 举报
回复
用velocity框架

67,537

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧