FCKeditor编辑器之HelloWorld

发布时间:2020-06-22编辑:脚本学堂
本文介绍了FCKeditor编辑器中HelloWorld的例子,有需要的朋友参考下。

FCKeditor在使用过程中出现的一些问题。

使用步骤:
1),从FCKeditor的官网获取FCKeditor以及相关的jar包
2),将FCKeditor拷贝至webroot下面,将jar包添加到lib目录下.
配置web.xml:
 

复制代码 代码示例:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0" metadata-complete="true"> 
    <servlet> 
        <servlet-name>Connector</servlet-name> 
        <servlet-class> 
            net.fckeditor.connector.ConnectorServlet 
        </servlet-class> 
        <init-param> 
            <param-name>basedir</param-name> 
            <param-value>/UserFiles/</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
 
    <servlet-mapping> 
        <servlet-name>Connector</servlet-name> 
        <url-pattern>/fckeditor/editor/filemanager/connectors/*</url-pattern> 
    </servlet-mapping> 
</web-app> 
 

在classpath能找到的地方添加:fckeditor.properties文件,内容如下:
connector.userActionImpl=net.fckeditor.requestcycle.impl.UserActionImpl
3). 在页面上调用FCKeditor, 可以通过javascript调取,也可以通过jsp来调用
a).通过javascript:
 (1). 在页面中引入fckeditor.js. 
 

复制代码 代码示例:
   <script type="text/javascript" src="FCKEditor/fckeditor.js"></script>

 (2). 在期望出现FCKeditor的位置添加:
 

复制代码 代码示例:
<script type="text/javascript"> 
var oFCKeditor = new FCKeditor( 'FCKeditor1' ) ; 
oFCKeditor.BasePath = "/FCKeditor/FCKEditor/" ; 
oFCKeditor.Width="70%"; 
oFCKeditor.Height="400"; 
oFCKeditor.Value="初始化内容"; 
oFCKeditor.Create() ; 
</script> 
 

(3). 将FCKeditor置于form表单之中.
(4). 提交信息的处理.
在负责处理请求的Servlet或JSP中对所有的请求参数一一获取,
 

复制代码 代码示例:
request.setCharacterEncoding("utf-8"); 
Enumeration<String> params = (Enumeration<String>) request.getParameterNames(); 
String parameter; 
String content=""; 
while (params.hasMoreElements()) { 
    parameter = params.nextElement(); 
    String subcontent=request.getParameter(parameter).trim(); 
        content=content+subcontent; 

//根据需求进行进一步处理 
 

 b).通过JSP的自定义标签获取FCKeditor
 (1). 引入标签:

复制代码 代码示例:
<%@taglib prefix="FCK" uri="http://java.fckeditor.net"%>

 (2). 在适当的位置显示:(value的值不能为空,否则会NullPointerException)
 

复制代码 代码示例:
<FCK:editor instanceName="myeditor" basePath="/FCKEditor"  
initializing..."> 
</FCK:editor> 

 (2).关于数据的提交与处理的过程与a)中所说类似,不再重复

4.FCKeditor的自定义
在使用FCKeditor时,难免要增加一些自己的风格,去掉一些不必要的功能.
1). 在FCKeditor目录下创建myconfig.js. (虽然可以直接修改fckconfig.js, 但最好不要这么做), 在  
  fckconfig中指定自定义配置文件的位置. 出现在自定义配置文件中的配置项会覆盖掉fckconfig中的
  同名配置项.
2).通常需要自定义的内容:
 自定义 ToolbarSet, 去掉一些功能
 加上几种常用的字体
 修改 “回车” 和 “Shift + 回车” 的换行行为
 修改编辑区样式文件
 更换表情图片
5. FCKeditor的一些其他问题:
1). 上传中文名字的文件会出现乱码.
解决办法:
使用自定义的ConnectorServlet替换net.fckeditor.connector.ConnectorServlet, 在web.xml中将
 

复制代码 代码示例:
<servlet-class>指定为自定义的ConnectorServlet.  自定义的ConnectorServlet如下:
public class ConnectorServlet extends HttpServlet { 
 
    private static final long serialVersionUID = -5742008970929377161L; 
    private static final Logger logger = LoggerFactory.getLogger(ConnectorServlet.class); 
 
    public void init() throws ServletException, IllegalArgumentException { 
        String realDefaultUserFilesPath = getServletContext().getRealPath( 
                ConnectorHandler.getDefaultUserFilesPath()); 
 
        File defaultUserFilesDir = new File(realDefaultUserFilesPath); 
        UtilsFile.checkDirAndCreate(defaultUserFilesDir); 
 
        logger.info("ConnectorServlet successfully initialized!"); 
    } 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        logger.debug("Entering ConnectorServlet#doGet"); 
 
        response.setCharacterEncoding("UTF-8"); 
        response.setContentType("application/xml; charset=UTF-8"); 
        response.setHeader("Cache-Control", "no-cache"); 
        PrintWriter out = response.getWriter(); 
 
        String commandStr = request.getParameter("Command"); 
        String typeStr = request.getParameter("Type"); 
        String currentFolderStr = request.getParameter("CurrentFolder"); 
 
        logger.debug("Parameter Command: {}", commandStr); 
        logger.debug("Parameter Type: {}", typeStr); 
        logger.debug("Parameter CurrentFolder: {}", currentFolderStr); 
 
        XmlResponse xr; 
 
        if (!RequestCycleHandler.isEnabledForFileBrowsing(request)) 
            xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.NOT_AUTHORIZED_FOR_BROWSING); 
        else if (!CommandHandler.isValidForGet(commandStr)) 
            xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_COMMAND); 
        else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) 
            xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_TYPE); 
        else if (!UtilsFile.isValidPath(currentFolderStr)) 
            xr = new XmlResponse(XmlResponse.EN_ERROR, Messages.INVALID_CURRENT_FOLDER); 
        else { 
            CommandHandler command = CommandHandler.getCommand(commandStr); 
            ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr); 
 
            String typePath = UtilsFile.constructServerSidePath(request, resourceType); 
            String typeDirPath = getServletContext().getRealPath(typePath); 
 
            File typeDir = new File(typeDirPath); 
            UtilsFile.checkDirAndCreate(typeDir); 
 
            File currentDir = new File(typeDir, currentFolderStr); 
 
            if (!currentDir.exists()) 
                xr = new XmlResponse(XmlResponse.EN_INVALID_FOLDER_NAME); 
            else { 
 
                xr = new XmlResponse(command, resourceType, currentFolderStr, UtilsResponse 
                        .constructResponseUrl(request, resourceType, currentFolderStr, true, 
                                ConnectorHandler.isFullUrl())); 
 
                if (command.equals(CommandHandler.GET_FOLDERS)) 
                    xr.setFolders(currentDir); 
                else if (command.equals(CommandHandler.GET_FOLDERS_AND_FILES)) 
                    xr.setFoldersAndFiles(currentDir); 
                else if (command.equals(CommandHandler.CREATE_FOLDER)) { 
                    String newFolderStr = UtilsFile.sanitizeFolderName(request 
                            .getParameter("NewFolderName")); 
                    logger.debug("Parameter NewFolderName: {}", newFolderStr); 
 
                    File newFolder = new File(currentDir, newFolderStr); 
                    int errorNumber = XmlResponse.EN_UKNOWN; 
 
                    if (newFolder.exists()) 
                        errorNumber = XmlResponse.EN_ALREADY_EXISTS; 
                    else { 
                        try { 
                            errorNumber = (newFolder.mkdir()) ? XmlResponse.EN_OK 
                                    : XmlResponse.EN_INVALID_FOLDER_NAME; 
                        } catch (SecurityException e) { 
                            errorNumber = XmlResponse.EN_SECURITY_ERROR; 
                        } 
                    } 
                    xr.setError(errorNumber); 
                } 
            } 
        } 
 
        out.print(xr); 
        out.flush(); 
        out.close(); 
        logger.debug("Exiting ConnectorServlet#doGet"); 
    } 
 
    @SuppressWarnings("unchecked") 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        logger.debug("Entering Connector#doPost"); 
 
        response.setCharacterEncoding("UTF-8"); 
        response.setContentType("text/html; charset=UTF-8"); 
        response.setHeader("Cache-Control", "no-cache"); 
        PrintWriter out = response.getWriter(); 
 
        String commandStr = request.getParameter("Command"); 
        String typeStr = request.getParameter("Type"); 
        String currentFolderStr = request.getParameter("CurrentFolder"); 
 
        logger.debug("Parameter Command: {}", commandStr); 
        logger.debug("Parameter Type: {}", typeStr); 
        logger.debug("Parameter CurrentFolder: {}", currentFolderStr); 
 
        UploadResponse ur; 
 
        // if this is a QuickUpload request, 'commandStr' and 'currentFolderStr' 
        // are empty 
        if (Utils.isEmpty(commandStr) && Utils.isEmpty(currentFolderStr)) { 
            commandStr = "QuickUpload"; 
            currentFolderStr = "/"; 
        } 
 
        if (!RequestCycleHandler.isEnabledForFileUpload(request)) 
            ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR, null, null, 
                    Messages.NOT_AUTHORIZED_FOR_UPLOAD); 
        else if (!CommandHandler.isValidForPost(commandStr)) 
            ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_COMMAND); 
        else if (typeStr != null && !ResourceTypeHandler.isValid(typeStr)) 
            ur = new UploadResponse(UploadResponse.SC_ERROR, null, null, Messages.INVALID_TYPE); 
        else if (!UtilsFile.isValidPath(currentFolderStr)) 
            ur = UploadResponse.UR_INVALID_CURRENT_FOLDER; 
        else { 
            ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr); 
 
            String typePath = UtilsFile.constructServerSidePath(request, resourceType); 
            String typeDirPath = getServletContext().getRealPath(typePath); 
 
            File typeDir = new File(typeDirPath); 
            UtilsFile.checkDirAndCreate(typeDir); 
 
            File currentDir = new File(typeDir, currentFolderStr); 
 
            if (!currentDir.exists()) 
                ur = UploadResponse.UR_INVALID_CURRENT_FOLDER; 
            else { 
 
                String newFilename = null; 
                FileItemFactory factory = new DiskFileItemFactory(); 
                ServletFileUpload upload = new ServletFileUpload(factory); 
                                //1 
                try { 
                    List<FileItem> items = upload.parseRequest(request); 
                    FileItem uplFile = items.get(0); 
                    String rawName = UtilsFile.sanitizeFileName(uplFile.getName()); 
                    String filename = FilenameUtils.getName(rawName); 
                    String baseName = FilenameUtils.removeExtension(filename); 
                    String extension = FilenameUtils.getExtension(filename); 
                    if (!ExtensionsHandler.isAllowed(resourceType, extension)) 
                        ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); 
                    else { 
                        File pathToSave = new File(currentDir, filename); 
                        int counter = 1; 
                        while (pathToSave.exists()) { 
                            newFilename = baseName.concat("(").concat(String.valueOf(counter)) 
                                    .concat(")").concat(".").concat(extension); 
                            pathToSave = new File(currentDir, newFilename); 
                            counter++; 
                        } 
                        if (Utils.isEmpty(newFilename)) 
                            ur = new UploadResponse(UploadResponse.SC_OK, UtilsResponse 
                                    .constructResponseUrl(request, resourceType, currentFolderStr, 
                                            true, ConnectorHandler.isFullUrl()).concat(filename)); 
                        else 
                            ur = new UploadResponse(UploadResponse.SC_RENAMED, 
                                    UtilsResponse.constructResponseUrl(request, resourceType, 
                                            currentFolderStr, true, ConnectorHandler.isFullUrl()) 
                                            .concat(newFilename), newFilename); 
 
                        // secure image check 
                        if (resourceType.equals(ResourceTypeHandler.IMAGE) 
                                && ConnectorHandler.isSecureImageUploads()) { 
                            if (UtilsFile.isImage(uplFile.getInputStream())) 
                                uplFile.write(pathToSave); 
                            else { 
                                uplFile.delete(); 
                                ur = new UploadResponse(UploadResponse.SC_INVALID_EXTENSION); 
                            } 
                        } else 
                            uplFile.write(pathToSave); 
 
                    } 
                } catch (Exception e) { 
                    ur = new UploadResponse(UploadResponse.SC_SECURITY_ERROR); 
                } 
            } 
        } 
        out.print(ur); 
        out.flush(); 
        out.close(); 
        logger.debug("Exiting Connector#doPost"); 
    } 

 

在原始的ConnectorServlet中找到doPost方法,并在第150行的//1处添加:
upload.setHeaderEncoding("utf-8")即可。