最近在学习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配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Sttuts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 监听器Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 定位applicationContext.xml的物理位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
5、applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="applicationContext_bean.xml"/>
<import resource="applicationContext_db.xml"/>
</beans>
6、在news.model下编写News.java类
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;
}
}
7、生成对应的映射文件,其中的大部分是使用String类型的,”内容“字段”content“在数据库中是以BLOB存放,非常的与此不同要注意。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-7-5 15:57:38 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="news.model.News" table="NEWS">
<id name="id" type="java.lang.String">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="newsid" type="java.lang.String">
<column name="NEWSID" />
</property>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="content" >
<column name="content" />
</property>
<property name="times" type="java.util.Date">
<column name="TIMES" />
</property>
<property name="types" type="java.lang.String">
<column name="TYPES" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="department" type="java.lang.String">
<column name="DEPARTMENT" />
</property>
</class>
</hibernate-mapping>
8、在news.service下编辑接口NewsService.java