天天看点

在JSP中配置FCKeditor 2.6.4

1.FCKeditor 介绍

FCKeditor 这个开源的HTML 文本编辑器可以让web 程序拥有如MS Word 这样强大的编辑功能,.FCKeditor 支持当前流行的浏览器。

2.准备工作:

环境:winddows XP、tomcat6.0、JDK1.6

下载:

1):FCKeditor_2.6.4.zip

地址:http://nchc.dl.sourceforge.net/sourceforge/fckeditor/FCKeditor_2.6.4.zip

2):fckeditor-java-2.4.1-bin.zip (JAVA支持包)地址http://nchc.dl.sourceforge.net/sourceforge/fckeditor/fckeditor-java-2.4.1-bin.zip

3):slf4j-1.5.2.zip 地址 :http://www.slf4j.org/dist/slf4j-1.5.2.zip

3.安装:

下面以jsp为例:

分别解压之后,我们可以得到一个fckeditor和fckeditor-java-2.4.1两个文件夹。fckeditor文件夹下是需要调用的页面和js文件等等,有各种版本,无所谓啦,我们之需要jsp就够了。将文件加全部复制到工程目录下等待调用即可。

注意:有点麻烦的是导包的问题。我们一共需要5个包:commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,fckeditor-java-core-2.4.1.jar,slf4j-api-1.5.6.jar,slf4j-simple-1.5.6.jar或slf4j-jdk14-1.5.6.jar。

上面前四个包都可以在fckeditor-java-2.4.1文件夹下面找到,但是第五个却要另外去找,这点我非常不理解,为什么不放在一起。

如果没有的话编译时就会出现如下错误信息:

严重: Servlet /fckeditorDemo threw load() exception

java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

当然版本或许不同,以上的版本是截止2009-02-4的最新版本。如果想要最新版本,可以在slf4j的官网http://www.slf4j.org/download.html下到。但是要注意,截止到2009-2-4,slf4j官方最新版本是1.5.6,但是fckeditor提供的slf4j-api却是1.5.2版本,如果两个版本不一样的话,你将会在控制台看到如下的消息:

严重: Servlet /Java threw load() exception

java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class

org.slf4j.LoggerFactory

所以千万要注意版本一致问题。如果你实在觉得下载很麻烦,那就到这里下载吧:http://www.slf4j.org/download.html

4.配置

1)在工程目录src/下新建一个文件fckeditor.properties,添加内容:

connector.userFilesPath=UploadFile

connector.userActionImpl=net.fckeditor.requestcycle.impl.UserActionImpl

其中第一行为重新定义上传的文件夹,默认文件夹为userfile,保存即可。

2)修改web.xml,用来提供上传功能支持

<servlet>                                          

      <servlet-name>Connector</servlet-name>       

        <servlet-class>                            

          net.fckeditor.connector.ConnectorServlet 

      </servlet-class>                             

      <load-on-startup>1</load-on-startup>         

</servlet>                                         

<servlet-mapping>                                  

      <url-pattern>                                

        /fckeditor/editor/filemanager/connectors/* 

      </url-pattern>                               

</servlet-mapping>                            

5.应用,建立一JSP文件如下

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String content=request.getParameter("edt1");

if (content != null) {

  content = content.replaceAll("\r\n", "");

  content = content.replaceAll("\r", "");

  content = content.replaceAll("\n", "");

  content = content.replaceAll("\"", "'");

}else{

  content = "";

}

//下面是处理中文内容的编码转换

content = new String(content.getBytes("iso8859-1"),"utf-8");

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    <title>FCKEditor 测试</title>

  </head>

  <script type="text/javascript" src="fckeditor/fckeditor.js"></script>

  <body>

    This is my JSP page. <br>

    <form method="post" name="frm1">

    <script type="text/javascript">

        var oFCKeditor = new FCKeditor("edt1");

        oFCKeditor.BasePath = "fckeditor/";

        oFCKeditor.Height='400';

        oFCKeditor.Value="<%=content%>";

        oFCKeditor.Create();

    </script>

    <input type="submit" value="提交">

    </form>

    <hr>

    <%=content%>

  </body>

</html>

启动服务器,用浏览器访问即可看到结果

继续阅读