jsp 產生靜態頁面代碼

來源:互聯網
上載者:User

網頁特效phttp://www.111cn.net/網頁特效p.html target=_blank >jsp教程 產生靜態頁面代碼

buildhtml.java:

import java.util.*;
import java.io.*;
public class htmlfile{
public static void main(string[] args){
try{
string title="測試";
string content="測試"
string editer="";
string filepath = "";
filepath ="template.html";
system.out.print(filepath);
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);
system.out.print(templatecontent);
templatecontent=templatecontent.replaceall("###title###",title);
templatecontent=templatecontent.replaceall("###content###",content);
templatecontent=templatecontent.replaceall("###author###",editer);// 替換掉模板中相應的地方
system.out.print(templatecontent);

// 根據時間得檔案名稱
calendar calendar = calendar.getinstance();
string fileame = string.valueof(calendar.gettimeinmillis()) +".html";
fileame = "/" + fileame;// 產生的html檔案儲存路徑。
fileoutputstream fileoutputstream = new fileoutputstream(fileame);// 建立檔案輸出資料流
system.out.print("檔案輸出路徑:");
system.out.print(fileame);
byte tag_bytes[] = templatecontent.getbytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}catch(exception e){
system.out.print(e.tostring());
}
}
}
%>
模板檔案

<html>
<head>
<title>${title} </title>
</head>
<body>
${content}
</body>
</html>

另一種方法 freemarker

用freemarker是這樣的原理:
1.擷取模板:也就是你事先必須要寫好模板範例。再次調用這個模板的時候,只是從緩衝中去讀取而已,
  不會再去對這個模板進行執行個體化template temp = cfg.gettemplate("test.ftl");
  gettemplate方法。
2.把模板與資料模型結合:資料模型+模版=輸出,而我們一旦擁有資料模型(root)和一個模版
  (template)那麼我們就可以把他們合并獲得輸出。
以上這個過程是通過template 類的process 方法來實現的,該方法需要兩個參數一個是
表示資料模型的root 一個表示輸出的writer。它把解析過的檔案輸出到writer 上。簡單起
見,我把輸出指定到了控制台(標準輸出)
writer out = new outputstreamwriter(system.out);
temp.process(root, out);
out.flush();

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.