搜索
首页web前端html教程html实现页面静态化的案例

html实现页面静态化的案例

Oct 24, 2017 am 10:13 AM
html博客实现

静态化文件位置注意:

wKioL1m2KwSD3R_pAAAjiwpj0zM659.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<News> getAllNews(){
		CharStreamIO io = new CharStreamIO();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		
		List<News> newsList = new ArrayList<News>();
		
		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<News> newsList = dao.getAllNews();
		String destPath = "/News/newspages";
		for(int i=0;i<newsList.size();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();
		}
	}
}

以上是html实现页面静态化的案例的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
HTML的未来:网络设计的发展和趋势HTML的未来:网络设计的发展和趋势Apr 17, 2025 am 12:12 AM

HTML的未来充满了无限可能。1)新功能和标准将包括更多的语义化标签和WebComponents的普及。2)网页设计趋势将继续向响应式和无障碍设计发展。3)性能优化将通过响应式图片加载和延迟加载技术提升用户体验。

HTML与CSS vs. JavaScript:比较概述HTML与CSS vs. JavaScript:比较概述Apr 16, 2025 am 12:04 AM

HTML、CSS和JavaScript在网页开发中的角色分别是:HTML负责内容结构,CSS负责样式,JavaScript负责动态行为。1.HTML通过标签定义网页结构和内容,确保语义化。2.CSS通过选择器和属性控制网页样式,使其美观易读。3.JavaScript通过脚本控制网页行为,实现动态和交互功能。

HTML:是编程语言还是其他?HTML:是编程语言还是其他?Apr 15, 2025 am 12:13 AM

HTMLISNOTAPROGRAMMENGUAGE; ITISAMARKUMARKUPLAGUAGE.1)htmlStructures andFormatSwebContentusingtags.2)itworkswithcsssforstylingandjavascript for Interactivity,增强WebevebDevelopment。

HTML:建立网页的结构HTML:建立网页的结构Apr 14, 2025 am 12:14 AM

HTML是构建网页结构的基石。1.HTML定义内容结构和语义,使用、、等标签。2.提供语义化标记,如、、等,提升SEO效果。3.通过标签实现用户交互,需注意表单验证。4.使用、等高级元素结合JavaScript实现动态效果。5.常见错误包括标签未闭合和属性值未加引号,需使用验证工具。6.优化策略包括减少HTTP请求、压缩HTML、使用语义化标签等。

从文本到网站:HTML的力量从文本到网站:HTML的力量Apr 13, 2025 am 12:07 AM

HTML是一种用于构建网页的语言,通过标签和属性定义网页结构和内容。1)HTML通过标签组织文档结构,如、。2)浏览器解析HTML构建DOM并渲染网页。3)HTML5的新特性如、、增强了多媒体功能。4)常见错误包括标签未闭合和属性值未加引号。5)优化建议包括使用语义化标签和减少文件大小。

了解HTML,CSS和JavaScript:初学者指南了解HTML,CSS和JavaScript:初学者指南Apr 12, 2025 am 12:02 AM

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

HTML的角色:构建Web内容HTML的角色:构建Web内容Apr 11, 2025 am 12:12 AM

HTML的作用是通过标签和属性定义网页的结构和内容。1.HTML通过到、等标签组织内容,使其易于阅读和理解。2.使用语义化标签如、等增强可访问性和SEO。3.优化HTML代码可以提高网页加载速度和用户体验。

HTML和代码:仔细观察术语HTML和代码:仔细观察术语Apr 10, 2025 am 09:28 AM

htmlisaspecifictypefodyfocusedonstructuringwebcontent,而“代码” badlyLyCludEslanguagesLikeLikejavascriptandPytyPythonForFunctionality.1)htmldefineswebpagertuctureduseTags.2)“代码”代码“ code” code code code codeSpassSesseseseseseseseAwiderRangeLangeLangeforLageforLogageforLogicIctInterract

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)