配置脚本创建编辑器
步骤:
(1)到官方下载CKEditor项目,地址http://ckeditor.com/download
如上图所示,有三种类型的编辑器可以选择
a. Basic package 基础版
b. Standard package 标准版
c. Full package 完整版
(2)使用eclipse或MyEclipse创建Web项目,加入CKEditor项目
下面以CKEditor的基础版为例:
解压官方网站下载的CKEditor,会有一个ckeditor文件夹,将其复制到WebRoot目录中,
并创建2个JSP页面:index.jsp(演示CKEditor)和sample_posteddata.jsp(接收数据)
如下图:
index.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- 1.导入核心JS --> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <title>Insert title here</title> </head> <body> <form action="sample_posteddata.jsp" method="post"> <p> <label for="editor1">Editor1:</label> </p> <!--2.创建一个文本哉 --> <textarea rows="10" cols="80" id="editor1" name="editor1"></textarea> <!--3.将上面创建的文本域替换成CKEditor编辑器(注:参数是按name去查找,找不到再找ID标识) --> <script type="text/javascript"> CKEDITOR.replace("editor1"); </script> <p> <input type="submit" value="submit"/> </p> </form> </body> </html>
sample_posteddata.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 处理下中文编码 --> <%request.setCharacterEncoding("UTF-8"); %> The Editor Value is : <p> <%=request.getParameter("editor1") %> </p> </body> </html>
CKEditor的基本原理:通过脚本动态生成HTML代码捆绑到文本域中,如下面图解:
提交结果:
现在去掉index.jsp中的编辑器捆绑代码
<script type="text/javascript"> //CKEDITOR.replace("editor1"); </script>
重新刷新浏览器中的index.jsp
PS:
关于CKEditor的各种编辑器类型和相关功能,可查考官方学习:http://ckeditor.com/demo#standard