html实现页面静态化的案例

黄舟
풀어 주다: 2017-10-24 10:13:17
원래의
3747명이 탐색했습니다.

静态化文件位置注意:

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 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
로그인 후 복사

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!