头部html 静态化页面,html实现页面静态化的案例

静态化文件位置注意:

08482defa2d58616cea425833085496a.png

实体类定义:public class News {

private String title;

private String pubTime;

private String category;

private String newsContent;

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getPubTime() {

return pubTime;

}

public void setPubTime(String pubTime) {

this.pubTime = pubTime;

}

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

public String getNewsContent() {

return newsContent;

}

public void setNewsContent(String newsContent) {

this.newsContent = newsContent;

}

}

自定义流的工具类public class CharStreamIO {

public void copyFile(String fsrc,String fDest){

File file = new File(fDest);

if(file.exists()){

file.delete();

}

PrintWriter out = null;

BufferedReader in = null;

try {

in = new BufferedReader(new FileReader(fsrc));

out = new PrintWriter(new BufferedWriter(new FileWriter(fDest)));

String strRet;

while((strRet=in.readLine()) != null){

out.println(strRet);

out.flush();

}

} catch (Exception e) {

e.printStackTrace();

}finally{

if(in != null){

try {

in.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

if(out != null){

try {

out.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

}

/**

* 把传入的信息,保存成文件

* @param finfo 传入的文件内容信息

* @param fname 目标路径和文件名

*/

public void writeFile(String finfo,String fDest){

File file = new File(fDest);

if(file.exists()){

file.delete();

}

PrintWriter out = null;

try {

out = new PrintWriter(new BufferedWriter(new FileWriter(fDest)));

out.write(finfo);

out.flush();

} catch (Exception e) {

e.printStackTrace();

}finally{

if(out !=null){

out.close();

}

}

}

/**

* 读取文本型文件

* @param name

* @return

*/

public String readFile(String fname){

File file = new File(fname);

StringBuilder bild = new StringBuilder();

BufferedReader in = null;

if(file.exists()){

try {

in = new BufferedReader(new FileReader(fname));

String strRet;

while((strRet=in.readLine()) != null){

bild.append(strRet);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

if(in != null){

try {

in.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

}else{

System.out.println(fname + "不存在");

}

return bild.toString();

}

}

数据访问层public class NewsDao {

/**

* 读取数据库中要生成的新闻信息

* @return

*/

public List getAllNews(){

CharStreamIO io = new CharStreamIO();

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

List newsList = new ArrayList();

News n1 = new News();

n1.setCategory("sport");

String c1 = io.readFile("NewsInfo\\news1.txt");

n1.setNewsContent(c1);

n1.setPubTime(sd.format(new Date()));

n1.setTitle("深足教练组:说我们买球是侮辱 朱广沪常暗中支招");

News n2 = new News();

n2.setCategory("hot");

String c2 = io.readFile("\\NewsInfo\\news2.txt");

n2.setNewsContent(c2);

n2.setPubTime(sd.format(new Date()));

n2.setTitle("对对对发发发失误失误");

newsList.add(n1);

newsList.add(n2);

return newsList;

}

}

业务逻辑层public class NewsBiz {

/**

* 读取数据库中要生成的新闻信息

* @return

*/

public void createAllNews() throws Exception{

NewsDao dao = new NewsDao();

List newsList = dao.getAllNews();

String destPath = "/News/newspages";

for(int i=0;i

//读取模板

CharStreamIO io = new CharStreamIO();

String tmp = io.readFile("/News/news.tmp");

//替换模板中的参数数据

News n = newsList.get(i);

String newTmp;

newTmp = tmp.replace(TemplateParam.TITLE, n.getTitle());

newTmp = newTmp.replace(TemplateParam.CATEGORY,n.getCategory());

newTmp = newTmp.replace(TemplateParam.PUB_TIME,n.getPubTime());

newTmp = newTmp.replace(TemplateParam.CONTENT, n.getNewsContent());

//把替换后的内容保存成新闻页面

io.writeFile(newTmp, destPath + "/news-" + i + ".html");

}

}

}

TemplateParam类public class TemplateParam {

public static final String TITLE = "%{title}%";

public static final String CATEGORY = "%{category}%";

public static final String CONTENT = "%{newsContent}%";

public static final String PUB_TIME = "%{pubTime}%";

}

用户接口层public class NewsTest {

public static void main(String[] args) {

NewsBiz biz = new NewsBiz();

try {

biz.createAllNews();

System.out.println("新闻页面创建完毕!");

} catch (Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值