<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
?<head>
??<title>FCKeditor - JSP Sample</title>
??<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
??<meta name="robots" content="noindex, nofollow">
??<link href="../sample.css" rel="stylesheet" type="text/css" />
??<script type="text/javascript">
function FCKeditor_OnComplete( editorInstance )
{
?window.status = editorInstance.Description ;
}
??</script>
?</head>
?<body>
??<h1>FCKeditor - JSP - Sample 1</h1>
??This sample displays a normal HTML form with an FCKeditor with full features
??enabled.
??<hr>
<!--? 自己的 action 方式? -->
??<form action="sampleposteddata.jsp" method="get" target="_blank">
<%
//以下是修改的關鍵
FCKeditor oFCKeditor ;
oFCKeditor = new FCKeditor( request, "EditorDefault" ) ;//初始化的一些配置,第二個參數EditorDefault就是這個字段的名稱
oFCKeditor.setBasePath( "/FCKeditor/" ) ;//為了尋找相應的frame頁面:fckeditor.html;同時傳遞參數;這里設置為我們需要的路徑!
//oFCKeditor.setBasePath( "/nhoa/FCKeditor/" ) ;//我的例子,就是指明你的文件放在哪個目錄下
oFCKeditor.setValue( "ligang's example!" );//初始化頁面顯示的數據
out.println( oFCKeditor.create() ) ;//建立吧,看源碼很簡單!
%>
???<br>
???<input type="submit" value="Submit">
??</form>
?</body>
</html>
************************************jsp頁面***********************************
使用步驟:
(1)項目中import 這個包包:fckEditor.jar(將src的java代碼打包),需要看源碼也可以!
(2)修改你的jsp頁面,在需要添加該效果的地方模仿以上jsp頁面進行修改
(3)想想,好象沒有什么事了!
(4)試試吧!
5.分析實現原理
主要就是oFCKeditor.create()?,通過這個方法建立控件,在源代碼中可以看見
public String create() {
??StringBuffer strEditor=new StringBuffer();
??
??strEditor.append("<div>");
??String encodedValue=HTMLEncode(value);
???if(isCompatible()) {?//瀏覽器版本符合要求產生我們的控件
???strEditor.append("<input type=\"hidden\" id=\"" + instanceName + "\" name=\"" + instanceName + "\" value=\"" + encodedValue + "\">");
??
???strEditor.append(createConfigHTML());
???strEditor.append(createIFrameHTML());
??
??}
??else{//瀏覽器版本不符合要求,產生一個textarea,呵呵,也不失是一種彌補的方式
???strEditor.append("<TEXTAREA name=\"" + instanceName + "\" rows=\"4\" cols=\"40\" style=\"WIDTH: " + width + "; HEIGHT: " + height + "\" wrap=\"virtual\">"+encodedValue+"</TEXTAREA>");
??}
??strEditor.append("</div>");
??return strEditor.toString();
?}
???其實就是一個servelt向瀏覽器寫一些html語句,其中的createConfigHTML()就是通過配置文件config.js向控件傳遞一些參數,在頁面是通過hidden域來傳遞的,但是自己沒有找到這個文件的位置,希望明白的朋友可以告訴我......
createIFrameHTML()就是建立控件的方法,其中最重要的一個參數就是sLink,
String sLink=basePath + "editor/fckeditor.html?InstanceName=" + instanceName;它指明了控件數據的名稱就是instanceName,它有時在那里被用戶初始化的呢,呵呵,是在構造方法里面,FCKeditor 類有一個構造方法
?public FCKeditor(HttpServletRequest req, String parInstanceName){
??request=req;
??basePath = request.getContextPath() + "/FCKeditor/";
??instanceName=parInstanceName;
??oConfig = new FCKeditorConfigurations() ;
?}
我們在jsp頁面上,看到
oFCKeditor = new FCKeditor( request, "EditorDefault" ) ;//初始化的一些配置,第二個參數EditorDefault就是這個
變量
6.到這里,需要我們試用者了解的部分就這些了,剩下的工作就是控件自己實現了,呵呵,真的很簡單啊
總結:
使用就要做兩件事情:import fckEditor.jar,這個jar包是我自己通過開源的代碼自己編譯后打的
還有就是在需要的地方添加代碼,注意兩個地方,一個是選取合適的構造函數和構造函數參數,還有就是
設定好自己項目擺放FCKeditor文件的目錄,基本沒有什么問題,今天在工作的項目上使用了一下沒有什么問題.