(updated information about manual installation) |
(Reorganized the docuemntation) |
||
Line 1: | Line 1: | ||
{{#CUSTOMTITLE:CKEditor for Java Integration Guide}} | {{#CUSTOMTITLE:CKEditor for Java Integration Guide}} | ||
+ | = Installation = | ||
Adding CKEditor to your application is a two-step process that entails: | Adding CKEditor to your application is a two-step process that entails: | ||
# downloading standalone CKEditor and placing it in the web application directory, | # downloading standalone CKEditor and placing it in the web application directory, | ||
Line 30: | Line 31: | ||
<code>*</code> Please note that <code>''3.x.y''</code> denotes the version of a CKEditor release. | <code>*</code> Please note that <code>''3.x.y''</code> denotes the version of a CKEditor release. | ||
− | + | = Using the Tag on the Page = | |
+ | |||
+ | To start using <code><ckeditor></code> tags in your JSP page, you need to declare the CKEditor tag library inside it: | ||
+ | <source lang="html4strict"> | ||
+ | <%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %> | ||
+ | </source> | ||
+ | |||
CKEditor replaces the HTML <code>textarea</code> element(s) with its instance(s). Unless you are using the <code><ckeditor:editor></code> tag (which will be described later), the first thing you need is a JSP page with an HTML <code>textarea</code> element. | CKEditor replaces the HTML <code>textarea</code> element(s) with its instance(s). Unless you are using the <code><ckeditor:editor></code> tag (which will be described later), the first thing you need is a JSP page with an HTML <code>textarea</code> element. | ||
+ | === Replacing Selected Textarea Element === | ||
For the purpose of this article let us assume that the page looks like this: | For the purpose of this article let us assume that the page looks like this: | ||
<source lang="html4strict"> | <source lang="html4strict"> | ||
Line 53: | Line 61: | ||
As seen in the code above, the <code>action</code> attribute of the <code>form</code> element contains the <code>sample_posteddata.jsp</code> value. This is a <code>web.xml</code> mapping that is used in the samples and points to a servlet that prints the contents of CKEditor, when the page is submitted. | As seen in the code above, the <code>action</code> attribute of the <code>form</code> element contains the <code>sample_posteddata.jsp</code> value. This is a <code>web.xml</code> mapping that is used in the samples and points to a servlet that prints the contents of CKEditor, when the page is submitted. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
Next you have to add the CKEditor tag (<code><ckeditor:replace></code> in this case) to the page. It is recommended to add it at the end, just before the closing <code></body></code> tag: | Next you have to add the CKEditor tag (<code><ckeditor:replace></code> in this case) to the page. It is recommended to add it at the end, just before the closing <code></body></code> tag: | ||
<source lang="html4strict"> | <source lang="html4strict"> | ||
− | <ckeditor:replace | + | <ckeditor:replace replace="editor1" basePath="/ckeditor/" /> |
</source> | </source> | ||
Line 67: | Line 70: | ||
* <code>replace</code> – points to the name or ID of the HTML <code>textarea</code> element that is to be replaced with a CKEditor instance. | * <code>replace</code> – points to the name or ID of the HTML <code>textarea</code> element that is to be replaced with a CKEditor instance. | ||
* <code>basePath</code> – contains the path to the main CKEditor directory. | * <code>basePath</code> – contains the path to the main CKEditor directory. | ||
− | |||
− | |||
For the purpose of this document it is assumed that CKEditor is available in the <code>/ckeditor/</code> directory (<code><nowiki>http://example.com/ckeditor/</nowiki></code>). | For the purpose of this document it is assumed that CKEditor is available in the <code>/ckeditor/</code> directory (<code><nowiki>http://example.com/ckeditor/</nowiki></code>). | ||
Line 90: | Line 91: | ||
</p> | </p> | ||
</form> | </form> | ||
− | <ckeditor:replace | + | <ckeditor:replace replace="editor1" basePath="/ckeditor/" /> |
</body> | </body> | ||
</html> | </html> | ||
+ | </source> | ||
+ | |||
+ | === Replacing All Textarea Elements === | ||
+ | The <code><ckeditor:replaceAll></code> tag replaces all <code>textarea</code> elements available in the document with editor instances. Optionally it can replace only the <code>textarea</code> elements that have a CSS class equal to the value of the CKEditor <code>className</code> attribute. | ||
+ | <source lang="html4strict"> | ||
+ | <ckeditor:replaceAll basePath="/ckeditor/" className="myclass"/> | ||
+ | </source> | ||
+ | |||
+ | This tag changes all <code>textarea</code> elements with a CSS class of <code>myclass</code> to CKEditor instances. | ||
+ | |||
+ | === Creating a CKEditor Instance === | ||
+ | The <code><ckeditor:editor></code> tag creates a CKEditor instance. | ||
+ | <source lang="html4strict"> | ||
+ | <ckeditor:editor textareaAttributes="<%=attr %>" basePath="/ckeditor/" editor="editor1" value="Type here" /> | ||
+ | </source> | ||
+ | |||
+ | For this tag no HTML <code>textarea</code> element needs to be present on a page. It is created internally and immediately replaced with a CKEditor instance. The code above contains the following elements: | ||
+ | * <code>basePath</code> – contains the path to the main CKEditor directory. | ||
+ | * <code>editor</code> – is the name of the internal <code>textarea</code> element. | ||
+ | * <code>value</code> – is the default <code>textarea</code> element value. | ||
+ | * <code>textareaAttributes</code> – is a <code>java.util.Map</code> with textarea attribute names serving as map keys, and attribute values serving as map values. For example: | ||
+ | <source lang="html4strict"> | ||
+ | <% | ||
+ | Map<String, String> attr = new HashMap<String, String>(); | ||
+ | attr.put("rows", "8"); | ||
+ | attr.put("cols", "50"); | ||
+ | %> | ||
</source> | </source> | ||
Line 98: | Line 126: | ||
The list below presents some of the commonly used tag attributes. | The list below presents some of the commonly used tag attributes. | ||
− | + | === <code>basePath</code> === | |
− | + | <code>basePath</code> – contains the path to the main CKEditor directory. | |
− | + | ||
− | + | If, for example, CKEditor is available under <code><nowiki>http://example.com/3rdparty/ckeditor/</nowiki></code>, the <code>basePath</code> attribute should be set to <code>/3rdparty/ckeditor/</code>. | |
+ | |||
+ | === <code>config</code> === | ||
+ | <code>config</code> – this parameter contains the CKEditor configuration object. For the list of available options, please refer to the [http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html JavaScript API]. | ||
+ | |||
+ | Configuration options are stored in the <code>CKEditorConfig</code> object. They are added by using the <code>addConfigValue</code> method: | ||
<source lang="html4strict"> | <source lang="html4strict"> | ||
<% | <% | ||
− | CKEditorConfig | + | CKEditorConfig settings = new CKEditorConfig(); |
− | + | settings.addConfigValue("width","500"); | |
%> | %> | ||
+ | <ckeditor:replace replace="editor1" basePath="/ckeditor/" config="<%=settings %>" /> | ||
</source> | </source> | ||
Line 112: | Line 146: | ||
<source lang="html4strict"> | <source lang="html4strict"> | ||
<% | <% | ||
− | CKEditorConfig | + | CKEditorConfig settings = new CKEditorConfig(); |
− | + | settings.addConfigValue("toolbar","[[ 'Source', '-', 'Bold', 'Italic' ]]"); | |
%> | %> | ||
</source> | </source> | ||
Line 119: | Line 153: | ||
<source lang="html4strict"> | <source lang="html4strict"> | ||
<% | <% | ||
− | CKEditorConfig | + | CKEditorConfig settings = new CKEditorConfig(); |
List<List<String>> list = new ArrayList<List<String>>(); | List<List<String>> list = new ArrayList<List<String>>(); | ||
List<String> subList = new ArrayList<String>(); | List<String> subList = new ArrayList<String>(); | ||
Line 127: | Line 161: | ||
subList.add("Italic"); | subList.add("Italic"); | ||
list.add(subList); | list.add(subList); | ||
− | + | settings.addConfigValue("toolbar", list); | |
%> | %> | ||
</source> | </source> | ||
− | + | === <code>timestamp</code> === | |
− | + | <code>timestamp</code> – this parameter stores the value specific for a particular CKEditor release, which helps to avoid browser caching problems when a new client-side CKEditor version is uploaded to the server. | |
− | + | ||
+ | === <code>initialized</code> === | ||
+ | <code>initialized</code> – setting this parameter to <code>true</code> means that the <code>ckeditor.js</code> script from CKEditor is already included in the page and there is no need to add it again. Setting it to <code>false</code>, on the other hand, means that the <code>ckeditor.js</code> script should be added to the page. | ||
+ | |||
+ | === <code>events</code> === | ||
+ | <code>events</code> – this parameter stores the list of client-side event listeners and is of <code>com.ckeditor.EventHandler</code> type. | ||
+ | |||
+ | ==== Example: ==== | ||
+ | Firstly, an instance of the <code>EventHandler</code> has to be created. Then you can add events by using the <code>addEvent</code> method, where the first parameter is the CKEditor event keyword and the second one is the JavaScript function in the form of a concatenated string. | ||
<source lang="html4strict"> | <source lang="html4strict"> | ||
<% | <% | ||
Line 141: | Line 183: | ||
</source> | </source> | ||
− | + | In order to use the events on a page, the following expression can be added: | |
<source lang="html4strict"> | <source lang="html4strict"> | ||
<ckeditor:editor basePath="/ckeditor/" editor="editor1" events="<%=eventHandler %>"/> | <ckeditor:editor basePath="/ckeditor/" editor="editor1" events="<%=eventHandler %>"/> | ||
</source> | </source> | ||
− | + | === <code>globalEvents</code> === | |
+ | <code>globalEvents</code> – this parameter stores the list of client-side event listeners that are used by all CKEditor instances and is of <code>GlobalEventHandler</code> type. | ||
<source lang="html4strict"> | <source lang="html4strict"> | ||
<% | <% | ||
Line 154: | Line 197: | ||
</source> | </source> | ||
+ | == Setting configuration options in a Java class == | ||
Instances of the configuration, events, and global events can be created either in a scriptlet, or in a separated Java class. Here is an example: | Instances of the configuration, events, and global events can be created either in a scriptlet, or in a separated Java class. Here is an example: | ||
<source lang="java"> | <source lang="java"> | ||
Line 202: | Line 246: | ||
events="<%= ConfigurationHelper.createEventHandlers() %>" /> | events="<%= ConfigurationHelper.createEventHandlers() %>" /> | ||
</source> | </source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Revision as of 19:23, 2 May 2011
Installation
Adding CKEditor to your application is a two-step process that entails:
- downloading standalone CKEditor and placing it in the web application directory,
- downloading and installing the server-side integration (CKEditor for Java).
Adding Client-Side CKEditor Sources
Go to the official CKEditor download site and grab the latest version of CKEditor. Place it in the directory of your web application.
Adding Tag Library (ckeditor-java-core)
When you want to add the CKEditor library, you can choose between using Maven and adding it manually.
Using Maven2
If your application uses Maven, you can add the following dependency to your pom.xml
file:
<dependency> <groupId>com.ckeditor</groupId> <artifactId>ckeditor-java-core</artifactId> <version>3.x.y</version> </dependency>
Please note that 3.x.y
denotes the artifact version, like <version>3.6</version>
.
Since CKEditor releases are added to the central Maven repository, the specified version should be downloaded automatically.
Without Maven
If you do not use Maven, you have to add the CKEditor jar
library manually. Go to the CKEditor download site, download the ckeditor-java-core-3.x.y*.jar
file, and put it in your /WEB-INF/lib
directory.
*
Please note that 3.x.y
denotes the version of a CKEditor release.
Using the Tag on the Page
To start using <ckeditor>
tags in your JSP page, you need to declare the CKEditor tag library inside it:
<%@ taglib uri="http://ckeditor.com" prefix="ckeditor" %>
CKEditor replaces the HTML textarea
element(s) with its instance(s). Unless you are using the <ckeditor:editor>
tag (which will be described later), the first thing you need is a JSP page with an HTML textarea
element.
Replacing Selected Textarea Element
For the purpose of this article let us assume that the page looks like this:
<!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>
As seen in the code above, the action
attribute of the form
element contains the sample_posteddata.jsp
value. This is a web.xml
mapping that is used in the samples and points to a servlet that prints the contents of CKEditor, when the page is submitted.
Next you have to add the CKEditor tag (<ckeditor:replace>
in this case) to the page. It is recommended to add it at the end, just before the closing </body>
tag:
<ckeditor:replace replace="editor1" basePath="/ckeditor/" />
Please note that the CKEditor tag above contains two attributes:
-
replace
– points to the name or ID of the HTMLtextarea
element that is to be replaced with a CKEditor instance. -
basePath
– contains the path to the main CKEditor directory.
For the purpose of this document it is assumed that CKEditor is available in the /ckeditor/
directory (http://example.com/ckeditor/
).
Please note that other tag attributes are also available. Refer to the Common Tag Attributes section below for a full description.
Below is the full source code of our sample page:
<!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>
Replacing All Textarea Elements
The <ckeditor:replaceAll>
tag replaces all textarea
elements available in the document with editor instances. Optionally it can replace only the textarea
elements that have a CSS class equal to the value of the CKEditor className
attribute.
<ckeditor:replaceAll basePath="/ckeditor/" className="myclass"/>
This tag changes all textarea
elements with a CSS class of myclass
to CKEditor instances.
Creating a CKEditor Instance
The <ckeditor:editor>
tag creates a CKEditor instance.
<ckeditor:editor textareaAttributes="<%=attr %>" basePath="/ckeditor/" editor="editor1" value="Type here" />
For this tag no HTML textarea
element needs to be present on a page. It is created internally and immediately replaced with a CKEditor instance. The code above contains the following elements:
-
basePath
– contains the path to the main CKEditor directory. -
editor
– is the name of the internaltextarea
element. -
value
– is the defaulttextarea
element value. -
textareaAttributes
– is ajava.util.Map
with textarea attribute names serving as map keys, and attribute values serving as map values. For example:
<% Map<String, String> attr = new HashMap<String, String>(); attr.put("rows", "8"); attr.put("cols", "50"); %>
Common Tag Attributes
The list below presents some of the commonly used tag attributes.
basePath
basePath
– contains the path to the main CKEditor directory.
If, for example, CKEditor is available under http://example.com/3rdparty/ckeditor/
, the basePath
attribute should be set to /3rdparty/ckeditor/
.
config
config
– this parameter contains the CKEditor configuration object. For the list of available options, please refer to the JavaScript API.
Configuration options are stored in the CKEditorConfig
object. They are added by using the addConfigValue
method:
<% CKEditorConfig settings = new CKEditorConfig(); settings.addConfigValue("width","500"); %> <ckeditor:replace replace="editor1" basePath="/ckeditor/" config="<%=settings %>" />
The first parameter of this method is always the name of a configuration option in the form of a string. The second one is the value of this option for which a String, Boolean, Number, List, or Map object can be used. No matter what type is used, CKEditor will try to map each value to an acceptable form. For example, the code below:
<% CKEditorConfig settings = new CKEditorConfig(); settings.addConfigValue("toolbar","[[ 'Source', '-', 'Bold', 'Italic' ]]"); %>
is equal to:
<% CKEditorConfig settings = new CKEditorConfig(); List<List<String>> list = new ArrayList<List<String>>(); List<String> subList = new ArrayList<String>(); subList.add("Source"); subList.add("-"); subList.add("Bold"); subList.add("Italic"); list.add(subList); settings.addConfigValue("toolbar", list); %>
timestamp
timestamp
– this parameter stores the value specific for a particular CKEditor release, which helps to avoid browser caching problems when a new client-side CKEditor version is uploaded to the server.
initialized
initialized
– setting this parameter to true
means that the ckeditor.js
script from CKEditor is already included in the page and there is no need to add it again. Setting it to false
, on the other hand, means that the ckeditor.js
script should be added to the page.
events
events
– this parameter stores the list of client-side event listeners and is of com.ckeditor.EventHandler
type.
Example:
Firstly, an instance of the EventHandler
has to be created. Then you can add events by using the addEvent
method, where the first parameter is the CKEditor event keyword and the second one is the JavaScript function in the form of a concatenated string.
<% EventHandler eventHandler = new EventHandler(); eventHandler.addEvent("instanceReady","function (ev) { alert(\"Loaded: \" + ev.editor.name); }"); %>
In order to use the events on a page, the following expression can be added:
<ckeditor:editor basePath="/ckeditor/" editor="editor1" events="<%=eventHandler %>"/>
globalEvents
globalEvents
– this parameter stores the list of client-side event listeners that are used by all CKEditor instances and is of GlobalEventHandler
type.
<% GlobalEventHandler globalEventHandler = new GlobalEventHandler(); globalEventHandler.addEvent("dialogDefinition","function (ev) { alert(\"Loading dialog window: \" + ev.data.name); }"); %>
Setting configuration options in a Java class
Instances of the configuration, events, and global events can be created either in a scriptlet, or in a separated Java class. Here is an example:
package com.ckeditor.samples; import java.util.ArrayList; import java.util.List; import com.ckeditor.CKEditorConfig; import com.ckeditor.EventHandler; import com.ckeditor.GlobalEventHandler; public class ConfigurationHelper { public static CKEditorConfig createConfig() { CKEditorConfig config = new CKEditorConfig(); List<List<String>> list = new ArrayList<List<String>>(); List<String> subList = new ArrayList<String>(); subList.add("Source"); subList.add("-"); subList.add("Bold"); subList.add("Italic"); list.add(subList); config.addConfigValue("toolbar", list); config.addConfigValue("width","500"); return config; } public static EventHandler createEventHandlers() { EventHandler handler = new EventHandler(); handler.addEvent("instanceReady","function (ev) { alert(\"Loaded: \" + ev.editor.name); }"); return handler; } public static GlobalEventHandler createGlobalEventHandlers() { GlobalEventHandler handler = new GlobalEventHandler(); handler.addEvent("dialogDefinition","function (ev) { alert(\"Loading dialog window: \" + ev.data.name); }"); return handler; } }
To access this class on a JSP page you can use the following expression:
<ckeditor:replace replace="editor1" basePath="ckeditor/" config="<%= ConfigurationHelper.createConfig() %>" events="<%= ConfigurationHelper.createEventHandlers() %>" />