HTML에 페이지 정적화를 구현한 사례
静态化文件位置注意:
实体类定义:
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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











HTML의 테이블 테두리 안내. 여기에서는 HTML의 테이블 테두리 예제를 사용하여 테이블 테두리를 정의하는 여러 가지 방법을 논의합니다.

HTML 여백-왼쪽 안내. 여기에서는 HTML margin-left에 대한 간략한 개요와 코드 구현과 함께 예제를 논의합니다.

HTML의 Nested Table에 대한 안내입니다. 여기에서는 각 예와 함께 테이블 내에 테이블을 만드는 방법을 설명합니다.

HTML 테이블 레이아웃 안내. 여기에서는 HTML 테이블 레이아웃의 값에 대해 예제 및 출력 n 세부 사항과 함께 논의합니다.

HTML 입력 자리 표시자 안내. 여기서는 코드 및 출력과 함께 HTML 입력 자리 표시자의 예를 논의합니다.

HTML 순서 목록에 대한 안내입니다. 여기서는 HTML Ordered 목록 및 유형에 대한 소개와 각각의 예에 대해서도 설명합니다.

HTML에서 텍스트 이동 안내. 여기서는 Marquee 태그가 구문과 함께 작동하는 방식과 구현할 예제에 대해 소개합니다.

HTML onclick 버튼에 대한 안내입니다. 여기에서는 각각의 소개, 작업, 예제 및 다양한 이벤트의 onclick 이벤트에 대해 설명합니다.
