最近在学习KindEditor插件,一款蛮不错的编辑器插件。在此将心得写出来,希望对大家有所帮助。
最终实现的效果如图所示:
(1)在管理员页面manage.jsp点击”添加新闻“按钮。
(2)可以从网络上面复制粘贴一篇文章,也可以自己编写文章。现在测试图片从其他网站复制粘贴的情况。
(3)点击提交,提示提交成功后,进入主页面index.jsp,发现刚才编辑的文章已经显示出来。
(4)这时候点击标题,可以正常显示文章来。
(5)除此外还可以实习本地图片的上传,文档的上传下载等功能。还有对上传的文章进行修改等功能。
实现步骤:
1、在Eclipse或者Myeclipse中搭建KindEditor环境,可以查照我先前的博客的做法:
http://blog.csdn.net/lhq13400526230/article/details/9256301
2、在Oracle中设计一张表名为news的数据库表。之所以有主键和业务主键,是为了让业务和主键无关系
3、创建工程,工作的目录如图所示:包括控制层(Action)、模型层(model)、接口(Service)、接口的实现类(ServiceImpl)、Spring配置(applicationContext.xml)、Spring的数据库配置(applicationContext_db.xml)、Spring的依赖注入(applicationContext_bean.xml)、存放上传图片、文件的目录的文件夹attached、存放KindEditor插件的文件夹KindEditor-4.1.7、存放JSP页面的文件夹JSP、存放新闻主页的JSP--index.jsp、存放管理员首页的JSP---manage.jsp等等。
4、web.xml配置
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
package news.model;
import java.sql.Blob;
import java.util.Date;
public class News {
private String id;//主键
private String newsid;//新闻主键
private String title;//新闻标题
private byte[] content;//新闻内容
private Date times;//新闻发布时间
private String types;//新闻类型
private String author;//新闻作者
private String department;//新闻发布部门
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public String getNewsid() {
return newsid;
}
public void setNewsid(String newsid) {
this.newsid = newsid;
}
public Date getTimes() {
return times;
}
public void setTimes(Date times) {
this.times = times;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTypes() {
return types;
}
public void setTypes(String types) {
this.types = types;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
package news.service;
import java.util.List;
import news.model.News;
public interface NewsService {
public String saveNews(News news,String content) throws Exception;//保存编译的新闻
public List findNews(String newsid) throws Exception;//显示新闻内容
public List listNews(String type) throws Exception;//显示新闻列表
public void updateShowNews(News n) throws Exception;//更新新闻
}
package news.serviceImpl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import news.action.NewsAction;
import news.model.News;
import news.service.NewsService;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
public class NewsServiceImpl implements NewsService{
static Logger log = Logger.getLogger(NewsServiceImpl.class);
private SessionFactory sessionFactory;
//保存数据
public String saveNews(News news,String htmlData) throws Exception {
String uuid=UUID.randomUUID().toString();//UUID产生主键
byte content[]=htmlData.getBytes("utf-8");//String类型转化为Byte类型
log.info("信息:"+htmlData);
Date date=new Date();//产生时间
DateFormat datefor = new SimpleDateFormat("yyyy-MM-dd-HHmmssFFFF");//时间格式化
String neswid = datefor.format(date);
news.setId(uuid);//设置主键
news.setNewsid(neswid);//设置新闻主键
news.setTimes(date);//设置时间
news.setContent(content);//设置内容
this.sessionFactory.getCurrentSession().save(news);
return neswid;
}
//修改新闻
public void updateShowNews(News n) throws Exception {
this.sessionFactory.getCurrentSession().update(n);
}
//显示新闻的标题栏
public List listNews(String type) throws Exception {
String sql = " from News t where t.types=:types ";
Query query=this.sessionFactory.getCurrentSession().createQuery(sql);
query.setParameter("types", type);//新闻编号
return query.list();
}
//根据新闻业务主键newsid查询新闻
public List findNews(String newsid) throws Exception {
String sql = " from News t where t.newsid=:newsid ";
Query query=this.sessionFactory.getCurrentSession().createQuery(sql);
query.setParameter("newsid", newsid);//新闻编号
return query.list();
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:orcl
lhq
lhq
1
40
1800
2
0
2
1800
30
true
false
org.hibernate.dialect.Oracle10gDialect
news/model/News.hbm.xml
package news.action;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import news.model.News;
import news.service.NewsService;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class NewsAction {
static Logger log = Logger.getLogger(NewsAction.class);
private NewsService news_services;
private News news;
//保存编辑的新闻信息
public String saveNews() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String htmlData = request.getParameter("content1");//获取页面的数据
news_services.saveNews(news,htmlData);
return "saveNewsSuccess";
}
//获取要更新新闻的信息
public String updateContent() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String htmlData=request.getParameter("content1");
log.info("新闻的主键============="+news.getId());
log.info("新闻的内容============="+htmlData);
byte content[]=htmlData.getBytes("utf-8");
news.setContent(content);
news_services.updateShowNews(news);
return "updateContentSuccess";
}
//查找新闻的详细信息
public String showNews() throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
String newsid=news.getNewsid();//从页面获取要查看的新闻ID
List list=news_services.findNews(newsid);
News n=(News) list.get(0);
DateFormat datefor = new SimpleDateFormat("yyyy-MM-dd HH:mm");//时间格式化
String times = datefor.format(n.getTimes());
String CONTENT=new String(n.getContent());//将byte数组转化为String类型
String auhtor=n.getAuthor();
String title=n.getTitle();
String types=n.getTypes();
String department=n.getDepartment();
String id=n.getId();
String no=n.getNewsid();
request.setAttribute("id", id);
request.setAttribute("newsid", no);
request.setAttribute("department", department);
request.setAttribute("title", title);
request.setAttribute("types", types);
request.setAttribute("content", CONTENT);
request.setAttribute("times", times);
request.setAttribute("author", auhtor);
return "showNewsSuccess";
}
//分别罗列出各个新闻的列表
public String NewsList() throws Exception{
Map request = (Map) ActionContext.getContext().get("request");//属于
List list=news_services.listNews("国际新闻");
request.put("list_guoji", list);
List list2=news_services.listNews("国内新闻");
request.put("list_guonei", list2);
List list3=news_services.listNews("体育新闻");
request.put("list_tiyu", list3);
List list4=news_services.listNews("娱乐新闻");
request.put("list_yule", list4);
return "sucess";
}
public NewsService getNews_services() {
return news_services;
}
public void setNews_services(NewsService news_services) {
this.news_services = news_services;
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
}
/jsp/success.jsp
/jsp/show_news_content.jsp
/jsp/show_news_list.jsp
/jsp/manage_news_list.jsp
/jsp/manage_news_content.jsp
/jsp/updateSuccess.jsp
15、index.jsp用于新闻网站的首页
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
request.setCharacterEncoding("UTF-8");
%>
新闻主页
新闻频道
16、show_news_list.jsp用于显示查询到已经发表的新闻列表。显示的只是新闻的标题,不是内容。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request
.getParameter("content1") : "";
%>
新闻显示列表页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
%>
新闻内容
发布时间:<%=request.getAttribute("times")%>
作者:<%=request.getAttribute("author")%>
<%=request.getAttribute("content")%>
18、manage.jsp管理员的主页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
%>
管理员页面
19、add.jsp管理员用来添加新闻的页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
KindEditor JSP
<%!
private String htmlspecialchars(String str) {
str = str.replaceAll("&", "&");
str = str.replaceAll("<", "<");
str = str.replaceAll(">", ">");
str = str.replaceAll("\"", """);
return str;
}
%>
20、提示新闻添加成功的页面success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
成功页面
发布成功!!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
request.setCharacterEncoding("UTF-8");
%>
管理员页面
管理员操作
22、manage_news_list.jsp显示可以更新的新闻列表
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request
.getParameter("content1") : "";
%>
新闻管理页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
KindEditor JSP
21、updateSuccess.jsp提示新闻修改成功的页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
成功页面
修改成功!!
管理员的网址:http://localhost:8080/news/manage.jsp
普通用户的网址:http://localhost:8080/news/index.jsp