ckeditor的使用(行距,上传图片,字体)

来源:互联网 发布:ubuntu使用数据库 编辑:程序博客网 时间:2024/05/17 06:51

1、本人使用的CKEditor版本是4.2  在官网上下载个CKeditor  http://ckeditor.com/download,如果您只是简单的使用,可以下载一个简洁版

,如果是想功能完善一点,可以下载full版!



这里我下载的是Full  Package!   下载之后解压,直接添加到项目中的WebRoot下任意路径下!



2、页面引用:

   ①引入js:       

[javascript] view plain copy
  1. <script language="javascript" src="<%=request.getContextPath()%>/common/ckeditor/ckeditor.js" type="text/javascript"></script>    

我这里把ckeditor放在了WebRoot下的common目录下

  ②通过一个textarea  替换成编辑器:                        

[javascript] view plain copy
  1. <html:hidden property="oper" />                
  2. <textarea rows="30" cols="50" name="content"></textarea>   

  这里隐藏了一个对象字段,对应到数据库保存字段!主要用 于                                                                                回显

  ③js  创建ckeditor对应,引入编辑器:   

[javascript] view plain copy
  1. <script type="text/javascript">  
  2.            var editor = null;  
  3.            window.onload=function(){  
  4.                editor = CKEDITOR.replace('content');//替换textarea  
  5.                editor.setData(document.getElementsByName("oper")[0].value);//用于回显数据库数据  
  6.            }  
  7.   
  8.         </script>  

至此就可以用了



3、上传图片

点击编辑器中图标可以看到插入图片的窗口!可能我只是个新手,但是我还是感觉这块太不好用了只是有个【图片信息】、【链接】、【高级】;想插入图片非常麻烦,这里可以把 隐藏的【上传】功能按钮显示出来,但是具体的上传功能还是要咱们手写;

①把【上传】按钮显示出来:

首先,还是image.js这个文件,搜索“upload”可以找到这一段

id:'Upload',hidden:!0

实际上上传功能被隐藏了,把上面的!0改成false,或者是0,再打开编辑器,就能找到上传功能了。


②完成上传类:

[html] view plain copy
  1. <pre code_snippet_id="150343" snippet_file_name="blog_20140110_1_5783331" name="code" class="java">public class UploadAction extends Action{  
  2.       
  3.       
  4.     private FormFile upload;  
  5.   
  6.     public FormFile getUpload() {  
  7.         return upload;  
  8.     }  
  9.   
  10.     public void setUpload(FormFile upload) {  
  11.         this.upload = upload;  
  12.     }  
  13.   
  14.     public ActionForward execute(ActionMapping mapping,ActionForm form,  
  15.             HttpServletRequest request,HttpServletResponse response) throws Exception{  
  16.         PrintWriter out = response.getWriter();  
  17.         DiskFileUpload upload = new DiskFileUpload();    
  18.         upload.setHeaderEncoding("UTF-8");   
  19.         List list = upload.parseRequest(request);    
  20.         response.setCharacterEncoding("UTF-8");  
  21.         String realPath = request.getSession().getServletContext().getRealPath("/");  
  22.                
  23.         Iterator iterlist.iterator();       
  24.         while(iter.hasNext())       
  25.         {       
  26.             String callback = request.getParameter("CKEditorFuncNum");  
  27.             FileItem element = (FileItem) iter.next();   
  28.             String fileName = element.getName();  
  29.             String fileType = fileName.split("\\.")[1];  
  30.             String fileNamed = java.util.UUID.randomUUID().toString();  
  31.             fileNamed = fileNamed+"."+fileType;  
  32.             if(!fileType.equals("png")&&!fileType.equals("gif")&&!fileType.equals("jpg")&&!fileType.equals("jpeg")&&!fileType.equals("bmp")){  
  33.                 out.println("<script type=\"text/javascript\">");    
  34.                 out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",''," + "'文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)');");   
  35.                 out.println("</script>");   
  36.                 return null;  
  37.             }  
  38.             File file = new File(realPath+"UserFiles/",fileNamed);       
  39.             element.write(file);    
  40.             out.println("<script type=\"text/javascript\">");      
  41.             out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + "UserFiles/"+fileNamed+"','')");      
  42.             out.println("</script>");   
  43.         }       
  44.         return null;}  
  45.       
  46.   
  47. }</pre><br>  
  48. <br>  
  49. <pre></pre>  
  50. <pre></pre>  
  51. <pre></pre>  
  52. <pre></pre>  
我这里是用的struts1   写的一个action  这里  自己随意写个上传类servlet  action 都无所谓!只要ckeditor能找到就行

③ ckeditor  调用上传类:在config.js 中添加

[javascript] view plain copy
  1.     var pathName = window.document.location.pathname;  
  2.    //获取带"/"的项目名  
  3.    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);  
  4.    config.filebrowserImageUploadUrl = projectName+'/uploadAction.do'//固定路径  
  5. config.filebrowserUploadUrl=projectName+"/uploadAction.do";  
这里pathName指的是项目名!


④ 在ckeditor  字体中加 中文字体  :这是默认没有中文字体;  同样可以在config.js中添加:

[javascript] view plain copy
  1. config.font_names='宋体/宋体 ;黑体/黑体 ;仿宋/仿宋_GB2312 ;楷体/楷体_GB2312 ;隶书/隶书 ;幼圆/幼圆 ;微软雅黑/微软雅黑 ;'+ config.font_names;  
  2.       

这样就能显示中文字体了



4、添加行距:

①从百度上下载一个 ckeditor 行距包,解压放到ckeditor/plugins目录下。

②在config.js 中添加   

config.extraPlugins += (config.extraPlugins ? ',lineheight' : 'lineheight');//行距③(转载)

1. lineheight插件源码修改:
        1.原先的插件依赖于ckeditor/plugins/sytles插件,而ckeditor4.0是没有这个插件,所以如果直接使用该插件,会报错:
        Uncaught [CKEDITOR.resourceManager.load] Resource name "styles" was not found
 
因此,要修改插件中对styles的引用:
 
         a.在lineheight目录下的plugin.js中:
                    
         将
                
[javascript] view plain copy
  1. CKEDITOR.plugins.add('lineheight',  
  2.                    {  
  3.                %
原创粉丝点击