CKEditor for java 集成指南

环境要求

JRE 1.4 、Servlet 2.5、JSP 2.1

安装

添加CKEditor到项目需要两步:

  1. 下载CKEditor放在项目web目录下,
  2. 下载并安装CKEditor的集成包(CKEditor for Java).

添加CKEditor客户端

CKEditor官网获取最新的版本。把它放在您的web应用程序的目录。

添加标签库(ckeditor-java-core)

添加CKEditor库,可以选择使用Maven和手动添加。

使用 Maven2

如果你是maven项目,你可以在pom.xml中添加如下依赖

<dependency>
	<groupId>com.ckeditor</groupId>
	<artifactId>ckeditor-java-core</artifactId>
	<version>3.x.y</version>
</dependency>

3.x.y表示CKEditor发布的版本,比如3.5.2

不使用Maven

如果不使用maven,请到  这里  下载jar包,放到您的/WEB-INF/lib/目录下

在页面上使用标签

在jsp页面上使用<ckeditor>标签,需要指定标签库地址(uri),如下:

<%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %>@ taglib uri="http://ckeditor.com" prefix="ckeditor" %>

CKEditor使用他自身的实例来替换html的textarea元素,除非你使用<ckeditor:editor>标签(稍后介绍),首先你需要jsp页面的textarea元素。

替换指定的Textarea元素

假设页面如下:

<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<html>
	<body>
		<form action="sample_posteddata.jsp" method="get">
			<p>
				<label for="editor1">Editor 1:</label>
				<textarea cols="80" id="editor1" name="editor1" rows="10"></textarea>
			</p>
			<p>
				<input type="submit" value="Submit" />
			</p>
		</form>
	</body>	
</html>


一个html页面的form表单说明,不多做翻译。

接下来你需要添加一个CKEditor标签(这里我们使用<ckeditor:replace>)到页面,建议添加在页面的最后,</body>标签之前。

<ckeditor:replace replace="editor1" basePath="/ckeditor/" />ckeditor:replace replace="editor1" basePath="/ckeditor/" />

上面的CKEditor标签包含两个属性

  • replace – 指向html中textarea的id或name,被指向的textarea会被替换为CKEditor的实例。
  • basePath – 包含CKEditor的路径

在这篇文章中,假设CKEditor放在/ckeditor/目录(http://example.com/ckeditor/)

请注意,其他标记属性也可用(textarea的标记属性,不一定id或name)。请参考下面的通用标签属性部分完整的描述。

下面是示例的完整源代码:

<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %>
<html>
	<body>
		<form action="sample_posteddata.jsp" method="get">
			<p>
				<label for="editor1">Editor 1:</label>
				<textarea cols="80" id="editor1" name="editor1" rows="10"></textarea>
			</p>
			<p>
				<input type="submit" value="Submit" />
			</p>
		</form>
	<ckeditor:replace replace="editor1" basePath="/ckeditor/" />
	</body>	
</html>
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %>
<html>
	<body>
		<form action="sample_posteddata.jsp" method="get">
			<p>
				<label for="editor1">Editor 1:</label>
				<textarea cols="80" id="editor1" name="editor1" rows="10"></textarea>
			</p>
			<p>
				<input type="submit" value="Submit" />
			</p>
		</form>
	<ckeditor:replace replace="editor1" basePath="/ckeditor/" />
	</body>	
</html>

替换所有文本元素

< ckeditor:replaceAll >标签替换所有可用的textarea为CKEditor实例,

猜你喜欢

转载自blog.csdn.net/laoziyaonitian/article/details/43565115