CMS

时间:2023-03-09 23:58:33
CMS

一、任务简介:

  开发简单的CMS。在数据库中创建新闻数据库表news,包含(题目、作者、日期、正文等字段);创建HTML模板文件;读取数据库所有数据的信息,并使用新闻信息

替换模板文件中的占位符,从而每一条新闻生成一个HTML静态页面。

二、具体分析步骤及其代码

1.客户关系系统

CMS

2.准备连接使用的变量  以及配置BaseDao

package cn.cms.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet; public class BaseDao {
//将连接需要的信息都设置成该类的静态常量
//驱动字符串
public static final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//url
public static final String url="jdbc:sqlserver://localhost:1433;DatabaseName=cms"; public static final String username="sa"; //用户名 public static final String password=""; //密码 Connection con;
PreparedStatement stat;
//01.获取连接对象的方法
public Connection getConnection() throws Exception{
//快乐星球
Class.forName(driver);
if(con==null||con.isClosed()){
con= DriverManager.getConnection(url,username,password);
}
return con;
} //02.对所有select语句执行的方法
public ResultSet executeQuery(String sql,Object...objs) throws Exception{
con=getConnection();
stat=con.prepareStatement(sql);
for (int i = ; i < objs.length; i++) {
stat.setObject(i+, objs[i]);
}
return stat.executeQuery();
}
}

3.形成entity

package cn.cms.entity;

public class News {
//ID, TITLE, AUTHOR, CREATETIME, CONTENT
private int ID;//新闻的ID
private String TITLE;//新闻标题
private String AUTHOR;//新闻的作者
private String CREATETIME;//时间
private String CONTENT;//新闻的内容 public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getTITLE() {
return TITLE;
}
public void setTITLE(String tITLE) {
TITLE = tITLE;
}
public String getAUTHOR() {
return AUTHOR;
}
public void setAUTHOR(String aUTHOR) {
AUTHOR = aUTHOR;
}
public String getCREATETIME() {
return CREATETIME;
}
public void setCREATETIME(String cREATETIME) {
CREATETIME = cREATETIME;
}
public String getCONTENT() {
return CONTENT;
}
public void setCONTENT(String cONTENT) {
CONTENT = cONTENT;
} }

4.由于DB端只有一张表News,所以需要在dao层创建一个INewsDao接口

package cn.cms.dao;

import java.util.List;

import cn.cms.entity.News;

public interface NewsDao {
//01.读取所有新闻列表的方法
public List<News> getAllNews() throws Exception; }

5.创建接口的实现类

package cn.cms.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List; import org.junit.Test; import cn.cms.dao.BaseDao;
import cn.cms.dao.NewsDao;
import cn.cms.entity.News; public class NewsDaoSQLServerImpl extends BaseDao implements NewsDao {
//单测
@Test
public void testSelect() throws Exception{
List<News> list= getAllNews();
for (News news : list) {
//System.out.print(news.getNewsTilte());
//ID, TITLE, AUTHOR, CREATETIME, CONTENT
System.out.println(news.getTITLE());
System.out.println(news.getAUTHOR());
System.out.println(news.getCREATETIME());
System.out.println(news.getCONTENT());
}
} @Override
public List<News> getAllNews() throws Exception {
List<News> list=new ArrayList<News>();
String sql="select * from NEWS";
//读取器
ResultSet rs = executeQuery(sql);
if(rs!=null){
while(rs.next()){
//如果有数据,数据表中的每条记录对应实体类的一个实例
News news=new News();
//ID, TITLE, AUTHOR, CREATETIME, CONTENT
//news.setID(rs.getInt("ID"));
news.setTITLE(rs.getString("TITLE"));
news.setAUTHOR(rs.getString("AUTHOR"));
news.setCREATETIME(rs.getString("CREATETIME"));
news.setCONTENT(rs.getString("CONTENT"));
list.add(news);
}
}
return list;
} }

6.service中读取dao中的泛型数据

package cn.cms.manager;

import java.util.List;

import cn.cms.dao.NewsDao;
import cn.cms.dao.impl.NewsDaoSQLServerImpl;
import cn.cms.entity.News;
import cn.cms.util.FileIO; public class NewsManager {
public void toHtml() throws Exception{
//读取模板文件内容,返回文件内容的字符串
FileIO fileio=new FileIO();
String templaterstr = fileio.readFile("E:\\news\\news.template");
//读取数据库表,获取新闻列表
NewsDao newsdao=new NewsDaoSQLServerImpl();
List<News> newslist=newsdao.getAllNews();
//替换模板文件,为每一条新闻创建一个HTML文件显示其信息
for (int i = ; i < newslist.size(); i++) {
//获取一条新闻
News news=newslist.get(i);
//使用该条新闻信息替换对应的占位符
String replacestr=new String();
replacestr=templaterstr;
replacestr=replacestr.replace("{title}",news.getTITLE());
replacestr=replacestr.replace("{author}", news.getAUTHOR());
replacestr=replacestr.replace("{createtime}", news.getCREATETIME());
replacestr=replacestr.replace("{content}", news.getCONTENT());
//为该条新闻生成HTML文件
String filePath="E:\\news\\news"+i+".html";
fileio.writeFile(filePath,replacestr);
}
}
}

7.借助工具类读取和写入流

package cn.cms.util;

import java.io.FileReader;

import java.io.FileWriter;

public class FileIO {

        public String readFile(String filePath) throws Exception{
//用字符流读取文本文件 FileReader reader=new FileReader(filePath);
//准备缓冲区
char[] chars=new char[];
int data;
String temp="";
while((data=reader.read(chars))!=-){
//char[] -----------------> String
temp=new String(chars);
System.out.println(temp);
}
return temp;
}
public void writeFile(String filePath,String str) throws Exception{
//字节流的写入
FileWriter writer=new FileWriter(filePath,true);
String word=str;
writer.write(word);
writer.close();
System.out.println("ok!!!");
}
}

8.在准备一个测试类就好了

package cn.cms.test;

import cn.cms.manager.NewsManager;

public class Test {
public static void main(String[] args) throws Exception {
NewsManager num=new NewsManager();
num.toHtml();
} }

HTML的模板

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>{title}</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gbk">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<table align="center" width="70%" border="">
<tr>
<td width="10%">
<b>标题:</b>
</td>
<td>
{title}
</td>
</tr>
<tr>
<td width="10%">
<b>作者:</b>
</td>
<td>
{author}
</td>
</tr>
<tr>
<td width="10%">
<b>时间:</b>
</td>
<td>
{createTime}
</td>
</tr>
<tr>
<td width="10%">
<b>内容:</b>
</td>
<td>
{content}
</td>
</tr>
</table>
</body>
</html>

总结:模板(template)和数据(Data)的隔离,以及重新整合,对学习

    和理解jsp有很大的帮助。