FCKeditor2.4(FCKeditot for java2.3)的用法及配置參數說明(已解決中文亂碼)

字體變小字體變大
&<60;&<60; 最近做的項目用到了,Fckeditor這個編譯器,用的還可以。今天用空整理一下。
&<60;&<60; 本文詳細介紹了Fckeditor的配置參數,並解決了提交中文的亂碼問題。是基於java使用Fckeditor的優秀指導文章,感謝原文作者。
&<60;&<60;
&<60;&<60; 一、轉載部分,FCKeditor配置:

1.下載
FCKeditor2.3 (FCKeditot for java)
FCKeditor2.4 (FCKeditor基本文件)
以下是下載地址:
http://www.fckeditor.net/download/default.html

2.建立項目:
建立項目tomcat/webapps/TestFCKeditor.

3.將FCKeditor2.4解壓縮
將FCKeditor2.4解壓縮,將整個目錄FCKeditor複製到項目的根目錄下,
目錄結構爲:tomcat/webapps/TestFCKeditor/fckeditor
然後將FCKeditor-2.3.zip(java)壓縮包中/web/WEB-INF/lib/目錄下的兩個jar文件拷到項目的/WEB-INF/ lib/目錄下。把其中的src目錄下的FCKeditor.tld文件copy到TestFCKedit/FCKeitor/WEB-INF/下

4.合併web.xml:
將FCKeditor-2.3.zip壓縮包中/web/WEB-INF/目錄下的web.xml文件合併到項目的/WEB-INF/目錄下的web.xml文件中。


5. 修改合併後的web.xml文件
修改合併後的web.xml文件,將名爲SimpleUploader的Servlet的enabled參數值改爲true,
以允許上傳功能,Connector Servlet的baseDir參數值用於設置上傳文件存放的位置。
添加標籤定義:

/TestFCKeditor
/WEB-INF/FCKeditor.tld


6. 映射:
上面文件中兩個servlet的映射分別爲:/editor/filemanager/browser/default/connectors/jsp/connector
和/editor/filemanager/upload/simpleuploader,需要在兩個映射前面加上/FCKeditor,
即改爲/fckeditor/editor/filemanager/browser/default/connectors/jsp/connector和
/fckeditor/editor/filemanager/upload/simpleuploader。
這兩個名字根據你放在工程中的FCKeditor文件夾名稱而定。。


7.修改skin文件夾
進入skin文件夾,如果你想使用fckeditor默認的這種奶黃色,
那就把除了default文件夾外的另兩個文件夾直接刪除.

8.刪除無用文件
刪除/FCKeditor/目錄下除fckconfig.js, fckeditor.js, fckstyles.xml, fcktemplates.xml四個文件以外的所有文件
刪除目錄/editor/_source,
刪除/editor/filemanager/browser/default/connectors/下的所有文件
刪除/editor/filemanager/upload/下的所有文件
刪除/editor/lang/下的除了fcklanguagemanager.js, en.js, zh.js, zh-cn.js四個文件的所有文件

9.修改配置:
打開/FCKeditor/fckconfig.js
修改 FCKConfig.DefaultLanguage = 'zh-cn' ;
把FCKConfig.LinkBrowserURL等的值替換成以下內容:
FCKConfig.LinkBrowserURL
= FCKConfig.BasePath + "filemanager/browser/default/browser.html?Connector=connectors/jsp/connector" ;

FCKConfig.ImageBrowserURL
= FCKConfig.BasePath + "filemanager/browser/default/browser.html?Type=Image&Connector=connectors/jsp/connector" ;

FCKConfig.FlashBrowserURL
= FCKConfig.BasePath + "filemanager/browser/default/browser.html?Type=Flash&Connector=connectors/jsp/connector" ;

FCKConfig.LinkUploadURL = FCKConfig.BasePath + 'filemanager/upload/simpleuploader?Type=File' ;
FCKConfig.FlashUploadURL = FCKConfig.BasePath + 'filemanager/upload/simpleuploader?Type=Flash' ;
FCKConfig.ImageUploadURL = FCKConfig.BasePath + 'filemanager/upload/simpleuploader?Type=Image' ;

10.其它
fckconfig.js總配置文件,可用記錄本打開,修改後將文件存爲utf-8 編碼格式。找到:

FCKConfig.TabSpaces = 0 ; 改爲FCKConfig.TabSpaces = 1 ; 即在編輯器域內可以使用Tab鍵。

如果你的編輯器還用在網站前臺的話,比如說用於留言本或是日記回覆時,那就不得不考慮安全了,
在前臺千萬不要使用Default的toolbar,要麼自定義一下功能,要麼就用系統已經定義好的Basic,
也就是基本的toolbar,找到:
FCKConfig.ToolbarSets["Basic"] = [
['Bold','Italic','-','OrderedList','UnorderedList','-',/*'Link',*/'Unlink','-','Style','FontSize','TextColor','BGColor','-',
'Smiley','SpecialChar','Replace','Preview'] ] ;
這是改過的Basic,把圖像功能去掉,把添加鏈接功能去掉,因爲圖像和鏈接和flash和圖像按鈕添加功能都能讓前臺
頁直接訪問和上傳文件, fckeditor還支持編輯域內的鼠標右鍵功能。

FCKConfig.ContextMenu = ['Generic',/*'Link',*/'Anchor',/*'Image',*/'Flash','Select','Textarea','Checkbox','Radio','TextField','HiddenField',
/*'ImageButton',*/'Button','BulletedList','NumberedList','TableCell','Table','Form'] ;

這也是改過的把鼠標右鍵的“鏈接、圖像,FLASH,圖像按鈕”功能都去掉。

  找到: FCKConfig.FontNames = 'Arial;Comic Sans MS;Courier New;Tahoma;Times New Roman;Verdana' ;
加上幾種我們常用的字體
FCKConfig.FontNames
= '宋體;黑體;隸書;楷體_GB2312;Arial;Comic Sans MS;Courier New;Tahoma;Times New Roman;Verdana' ;

11.添加文件
添加文件 /TestFCKeditor/test.jsp:
<%@ page language="java" import="com.fredck.FCKeditor.*" %>
<%@ taglib uri="/TestFCKeditor" prefix="FCK" %>
<script type="text/javascript" src="/TestFCKeditor/FCKeditor/fckeditor.js"></script>

<%--
三種方法調用FCKeditor
1.FCKeditor自定義標籤 (必須加頭文件 <%@ taglib uri="/TestFCKeditor" prefix="FCK" %> )
2.script腳本語言調用 (必須引用 腳本文件 <script type="text/javascript" src="/TestFCKeditor/FCKeditor/fckeditor.js"></script> )
3.FCKeditor API 調用 (必須加頭文件 <%@ page language="java" import="com.fredck.FCKeditor.*" %> )
--%>

//標籤調用方式
<%--
<form action="show.jsp" method="post" target="_blank">
<FCK:editor id="content" basePath="/TestFCKeditor/FCKeditor/"
width="700"
height="500"
skinPath="/TestFCKeditor/FCKeditor/editor/skins/silver/"
toolbarSet = "Default"
>
input
</FCK:editor>
<input type="submit" value="Submit">
</form>
--%>

//JS調用方式
<form action="show.jsp" method="post" target="_blank">
<table border="0" width="700"><tr><td>
<textarea id="content" name="content" style="WIDTH: 100%; HEIGHT: 400px">input</textarea>
<script type="text/javascript">
var oFCKeditor = new FCKeditor('content') ;
oFCKeditor.BasePath = "/TestFCKeditor/FCKeditor/" ;
oFCKeditor.Height = 400;
oFCKeditor.ToolbarSet = "Default" ;
oFCKeditor.ReplaceTextarea();
</script>
<input type="submit" value="Submit">
</td></tr></table>
</form>

//FCKeditor API 調用
<%--
<form action="show.jsp" method="post" target="_blank">
<%
FCKeditor oFCKeditor ;
oFCKeditor = new FCKeditor( request, "content" ) ;
oFCKeditor.setBasePath( "/TestFCKeditor/FCKeditor/" ) ;
oFCKeditor.setValue( "input" );
out.println( oFCKeditor.create() ) ;
%>
<br>
<input type="submit" value="Submit">
</form>
--%>

添加文件/TestFCKeditor/show.jsp:
<%
String content = request.getParameter("content");
out.print(content);
%>




12.測試
瀏覽http://localhost:8080/TestFCKeditor/test.jsp


最後注意。。不同的版本 變量名稱可能不一樣。請參考你使用的API文檔

配置選項:

AutoDetectLanguage=true/false 自動檢測語言
BaseHref="" 相對鏈接的基地址
ContentLangDirection="ltr/rtl" 默認文字方向
ContextMenu=字符串數組,右鍵菜單的內容
CustomConfigurationsPath="" 自定義配置文件路徑和名稱
Debug=true/false 是否開啓調試功能,這樣,當調用FCKDebug.Output()時,會在調試窗中輸出內容
DefaultLanguage="" 缺省語言
EditorAreaCss="" 編輯區的樣式表文件
EnableSourceXHTML=true/false 爲TRUE時,當由可視化界面切換到代碼頁時,把HTML處理成XHTML
EnableXHTML=true/false 是否允許使用XHTML取代HTML
FillEmptyBlocks=true/false 使用這個功能,可以將空的塊級元素用空格來替代
FontColors="" 設置顯示顏色拾取器時文字顏色列表
FontFormats="" 設置顯示在文字格式列表中的命名
FontNames="" 字體列表中的字體名
FontSizes="" 字體大小中的字號列表
ForcePasteAsPlainText=true/false 強制粘貼爲純文本
ForceSimpleAmpersand=true/false 是否不把&符號轉換爲XML實體
FormatIndentator="" 當在源碼格式下縮進代碼使用的字符
FormatOutput=true/false 當輸出內容時是否自動格式化代碼
FormatSource=true/false 在切換到代碼視圖時是否自動格式化代碼
FullPage=true/false 是否允許編輯整個HTML文件,還是僅允許編輯BODY間的內容
GeckoUseSPAN=true/false 是否允許SPAN標記代替B,I,U標記
IeSpellDownloadUrl=""下載拼寫檢查器的網址
ImageBrowser=true/false 是否允許瀏覽服務器功能
ImageBrowserURL="" 瀏覽服務器時運行的URL
ImageBrowserWindowHeight="" 圖像瀏覽器窗口高度
ImageBrowserWindowWidth="" 圖像瀏覽器窗口寬度
LinkBrowser=true/false 是否允許在插入鏈接時瀏覽服務器
LinkBrowserURL="" 插入鏈接時瀏覽服務器的URL
LinkBrowserWindowHeight=""鏈接目標瀏覽器窗口高度
LinkBrowserWindowWidth=""鏈接目標瀏覽器窗口寬度
Plugins=object 註冊插件
PluginsPath="" 插件文件夾
ShowBorders=true/false 合併邊框
SkinPath="" 皮膚文件夾位置
SmileyColumns=12 圖符窗列數
SmileyImages=字符數組 圖符窗中圖片文件名數組
SmileyPath="" 圖符文件夾路徑
SmileyWindowHeight 圖符窗口高度
SmileyWindowWidth 圖符窗口寬度
SpellChecker="ieSpell/Spellerpages" 設置拼寫檢查器
StartupFocus=true/false 開啓時FOCUS到編輯器
StylesXmlPath="" 設置定義CSS樣式列表的XML文件的位置
TabSpaces=4 TAB鍵產生的空格字符數
ToolBarCanCollapse=true/false 是否允許展開/摺疊工具欄
ToolbarSets=object 允許使用TOOLBAR集合
ToolbarStartExpanded=true/false 開啓是TOOLBAR是否展開
UseBROnCarriageReturn=true/false 當回車時是產生BR標記還是P或者DIV標記

解決上傳亂碼:
在SimpleUploaderServlet.java和ConnectorServlet.java兩個文件裏找到
DiskFileUpload upload = new DiskFileUpload();
分別在其後加入 upload.setHeaderEncoding("utf-8");
這樣解決了文件上傳的中文亂碼問題.
但是在控制檯顯示的中文內容還是亂碼,但是沒關係,我們沒必要去看控制檯下的中文

500錯誤:
&<60;&<60;&<60;&<60;&<60;&<60;&<60;&<60;&<60; 如果你上傳時,出現500的錯誤,試試加上serializer.jar、xalan.jar、commons-fileupload.jar三個包。
二、根據自己的需求修改:
&<60;&<60;&<60;&<60;&<60;&<60; 點擊上傳圖片,將圖片信息上傳到數據庫。
&<60;&<60;&<60;&<60;&<60; 修改connector/ConnectorServlet.java dopost/doget方法。
&<60; &<60; &<60;&<60; 我用的是post方式提交的:
&<60;
&<60; &<60; &<60; &<60; &<60;public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

&<60;&<60;&<60; &<60;&<60;&<60; if (debug) System.out.println("--- BEGIN DOPOST ---");

&<60;&<60;&<60; &<60;&<60;&<60; response.setContentType("text/html; charset=UTF-8");
&<60;&<60;&<60; &<60;&<60;&<60; response.setHeader("Cache-Control","no-cache");
&<60;&<60;&<60; &<60;&<60;&<60; PrintWriter out = response.getWriter();
&<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; String commandStr=request.getParameter("Command");
&<60;&<60;&<60; &<60;&<60;&<60; String typeStr=request.getParameter("Type");
&<60;&<60;&<60; &<60;&<60;&<60; String currentFolderStr=request.getParameter("CurrentFolder");
&<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; String currentPath=baseDir+typeStr+currentFolderStr;
//&<60;&<60;&<60; &<60;&<60;&<60; System.out.println(currentPath);
&<60;&<60;&<60; &<60;&<60;&<60; String currentDirPath=getServletContext().getRealPath(currentPath);
&<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; if (debug) System.out.println(currentDirPath);
&<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; String retVal="0";
&<60;&<60;&<60; &<60;&<60;&<60; String newName="";
&<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; if(!commandStr.equals("FileUpload"))
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; retVal="203";
&<60;&<60;&<60; &<60;&<60;&<60; else {
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; DiskFileUpload upload = new DiskFileUpload();
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; try {
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; List items = upload.parseRequest(request);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; Map fields=new HashMap();
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; Iterator iter = items.iterator();
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; while (iter.hasNext()) {
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; FileItem item = (FileItem) iter.next();
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; if (item.isFormField())
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; fields.put(item.getFieldName(),item.getString());
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; else
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; fields.put(item.getFieldName(),item);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; }
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; FileItem uplFile=(FileItem)fields.get("NewFile");
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; String fileNameLong=uplFile.getName();
//&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; System.out.println(fileNameLong);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; fileNameLong=fileNameLong.replace('//','/');
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; String[] pathParts=fileNameLong.split("/");
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; String fileName=pathParts[pathParts.length-1];
//&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; System.out.println(fileName+"ccccccccccc");
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; String nameWithoutExt=getNameWithoutExtension(fileName);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; System.out.println(nameWithoutExt);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; String ext=getExtension(fileName);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; System.out.println(ext);
//&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60; 上傳圖片信息,添加到數據庫中 開始
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;List <ImageMessage> imlist=new ArrayList<ImageMessage>();
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60; ImageMessage img=new ImageMessage();
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60; img.setImageName(fileName);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60; img.setImagePaht(currentDirPath);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60; imlist.add(img);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60; LinkDatabase link=new LinkDatabase();
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60; link.insertMessage(imlist);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; //結束
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; File pathToSave=new File(currentDirPath,fileName);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; int counter=1;
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; while(pathToSave.exists()){
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; newName=nameWithoutExt+"("+counter+")"+"."+ext;
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; retVal="201";
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; pathToSave=new File(currentDirPath,newName);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; counter++;
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; }
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; uplFile.write(pathToSave);
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; }catch (Exception ex) {
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; retVal="203";
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60; }
&<60;&<60;&<60; &<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; }
&<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; out.println("<script type=/"text/javascript/">");
&<60;&<60;&<60; &<60;&<60;&<60; out.println("window.parent.frames['frmUpload'].OnUploadCompleted("+retVal+",'"+newName+"');");
&<60;&<60;&<60; &<60;&<60;&<60; out.println("</script>");
&<60;&<60;&<60; &<60;&<60;&<60; out.flush();
&<60;&<60;&<60; &<60;&<60;&<60; out.close();
&<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60; if (debug) System.out.println("--- END DOPOST ---");&<60;&<60;&<60;
&<60;&<60;&<60; &<60;&<60;&<60;
&<60;&<60;&<60; }&<60; &<60; &<60;
三、動態的修改工具欄
&<60;&<60;&<60;&<60;
&<60;&<60;&<60;&<60;&<60;&<60; 修改fckconfig.js文件。
&<60;&<60; 在添加下面代碼,把“Default”,改爲自定義工具名稱。在jsp 頁面設置FCKeditor實例&<60;&<60;&<60;&<60;&<60; oFCKeditor.setToolbarSet("自定義工具名稱");

&<60;&<60;&<60;&<60; FCKConfig.ToolbarSets["Default"] = [
&<60;&<60;&<60; ['Source','DocProps','-','Save','NewPage','Preview','-','Templates'],
&<60;&<60;&<60; ['Cut','Copy','Paste','PasteText','PasteWord','-','Print','SpellCheck'],
&<60;&<60;&<60; ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
&<60;&<60;&<60; ['Form','Checkbox','Radio','TextField','Textarea','Select','Button','ImageButton','HiddenField'],
&<60;&<60; &<60;&<60;&<60; '/',
&<60;&<60;&<60; ['Style','FontFormat','FontName','FontSize'],
&<60;&<60;&<60; ['TextColor','BGColor'],
&<60;&<60;&<60; ['FitWindow','-','About']
] ;


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1685934

 
發佈了30 篇原創文章 · 獲贊 0 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章