服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 开发工具 > 查看文档

liferay中整合tinymce详解

摘要:

    最近在使用liferay开发一个门户网站的过程中遇到默认的在线文章编辑器无法满足用户需求的问题。liferay默认的文章编辑器功能比较简单,且可扩展性较差。经过再三权衡,决定采用tinymce作为liferay的默认在线文章编辑器。但是,官方下载的tinymce的advimage插件不具有图片上传的功能,需要对该插件进行扩展。经过反复琢磨,终于解决了以上的问题。现在把解决方法写出来,希望能给有需要的网友一点帮助。

   最近在使用liferay开发一个门户网站的过程中遇到默认的在线文章编辑器无法满足用户需求的问题。liferay默认的文章编辑器功能比较简单,且可扩展性较差。经过再三权衡,决定采用tinymce作为liferay的默认在线文章编辑器。但是,官方下载的tinymce的advimage插件不具有图片上传的功能,需要对该插件进行扩展。经过反复琢磨,终于解决了以上的问题。现在把解决方法写出来,希望能给有需要的网友一点帮助。

    需要注意的是早期的liferay-3.6.1集成tinymce存在问题,在ie浏览器环境下不能正确显示编辑器,显示“timymce为定义”错误。在firefox浏览器下可以正确显示,但是页面初始化时非常慢。经过很多尝试这些问题忍让没有解决,估计问题与liferay的页面缓存有关。另外在liferay-3.6.1环境下上传图片时会出现图片文件大小发生变化,且文件被破坏的问题。liferay的更新版本已经发布,且以上存在的问题可能与liferay本身的实现机制有关,解决这些问题可能比较棘手(呵呵,如果哪位大侠知道还望不吝赐教啊!),因此实现中使用的软件版本如下:
  tinymce:tinymce_2_0_8
   liferay:liferay-portal-tomcat-4.1.2

  在使用tinymce前先解决tinymce的中文化以及中文字体的使用问题。

版权声明:任何获得matrix授权的网站,转载时请务必保留以下作者信息和链接
作者:李乐鑫
原文:http://www.matrix.org.cn/resource/article/2006-11-19/liferay+tinymce_c6b2d4d0-7771-11db-bdce-bdc029e475a1.html
关键字:liferay;tinymce

(一)tinymce的中文化以及中文字体
    中文化方法:
    tinymce的中文化问题解决非常简单。首先到tinymce的官方下载中文语言包tinymce_lpackage_zh-cn.jar,并将其解压(假设解压后的路径为{$language_path})。然后将解压后的{$language_path}\tinymce\jscripts\tiny_mce目录下的所有文件复制到liferay安装路径(假设liferay的安装路径为{$liferay_home})的/webapps/root/html/js/editor/tinymce目录下,修改该路径下的timymce.jsp,在tynymce.init中增加配置项 language : "zh_cn"。

    中文字体设置方法:
    默认情况下,tinymce编辑工具栏的字体下拉框中没有中文字体,需要在下拉框中增加需要的中文字体。修改方法如下,分别修改{$liferay_home}/webapps/root /html/ js/editor/ tinymce/themes/  目录下的两个子目录advanced和simple目录下的editor_template.js。在var ifonts='…'和var nfonts='…'中增加中文字体,如增加“宋体=宋体;方正姚体=方正姚体”等。


(二)集成liferay和tinymce
     1.下载tinymce_2_0_8.zip,并解压(假设解压目录为{$tinymce_path})。然后将解压后的文件夹{$tinymce_path}\tinymce\jscripts\tiny_mce复制到{$liferay_home}\ \webapps\root\html\js\editor下,并改名为tinymce;
    2. 设置liferay应用的默认html编辑器为tinymce;(可以修改配置文件system.properties或直接修改{$liferay_home}\webapps\root\html\js\editor\editor.jsp); 修改后的editor.jsp如下:  

<%@ page import="com.liferay.portal.util.constants" %>
<%@ page import="com.liferay.portal.util.propsutil" %>
<%@ page import="com.liferay.util.browsersniffer" %>
<%@ page import="com.liferay.util.paramutil" %>
<%
string editorimpl = "simple";
if (browsersniffer.is_rtf(request)) {
editorimpl = paramutil.get(request, "editorimpl", propsutil.get(editor_wysiwyg_impl_key));
}
//editorimpl = "fckeditor";
//editorimpl = "liferay";
//editorimpl = "simple";
editorimpl = "tinymce";
// resin won't allow dynamic content inside the jsp:include tag
requestdispatcher rd = application.getrequestdispatcher(constants.text_html_dir +
"/js/editor/" + editorimpl + ".jsp");
rd.include(request, response);
%>
<%--<jsp:include page='<%= constants.text_html_dir + "/js/editor/" + editorimpl + ".jsp" %>' />--%>

 



(三)为tinymce增加图片上传功能
      本实现采用struts实现图片的上传。 步骤如下:
    1.编写文件上传类。代码清单如下:
     fileuploadaction.java代码清单如下:
    

package com.family.fileupload.web.action;
      import java.util.calendar;
      import java.util.date;
      import javax.servlet.http.httpservletrequest;
      import javax.servlet.http.httpservletresponse;
      import org.apache.struts.action.actionform;
      import org.apache.struts.action.actionforward;
      import org.apache.struts.action.actionmapping;
      import org.apache.struts.actions.dispatchaction;
      import com.family.fileupload.web.form.fileuploadform;
      import com.family.fileupload.web.utils.fileupload;
      public class fileuploadaction extends dispatchaction {
      public actionforward uploadimage(actionmapping mapping, actionform form,
          httpservletrequest request, httpservletresponse response) throws exception {
          fileuploadform uploadform = (fileuploadform) form;
          fileupload fu = new fileupload(uploadform.getfile());
          string realpath = this.getrealpath("/pictures");
          string filename = this.getfilekeyrule() + "." + fu.getextendname().tolowercase();
          string filepath = realpath + "/" + filename;
          fu.savetofile(filepath);
          request.setattribute("fileurl", this.getrooturl(request) + "/pictures/" + filename);
          return mapping.findforward("image.success");
      }
      private string getrooturl(httpservletrequest request) {
          stringbuffer buf = request.getrequesturl();
          int length = request.getrequesturi().length();
          buf.delete(buf.length() - length, buf.length());
          return buf.tostring();
      }
      private string getrealpath(string floder) {
          return this.getservlet().getservletconfig().getservletcontext().getrealpath(floder);
     }
     private string getfilekeyrule() {
          calendar cal = calendar.getinstance();
          cal.settime(new date());
          int iyear = cal.get(calendar.year);
          int imonth = cal.get(calendar.monday) + 1;
          int iday = cal.get(calendar.day_of_month);
          int ihour = cal.get(calendar.hour_of_day);
          int iminute = cal.get(calendar.minute);
          int isecond = cal.get(calendar.second);
          stringbuffer strkey = new stringbuffer(15);
          strkey.append(iyear);
          if (imonth < 10) {
              strkey.insert(4, '0');
              strkey.insert(5, imonth);
          } else {
              strkey.insert(4, imonth);
          }
          if (iday < 10) {
              strkey.insert(6, '0');
              strkey.insert(7, iday);
          } else {
              strkey.insert(6, iday);
          }
          if (ihour < 10) {
              strkey.insert(8, '0');
              strkey.insert(9, ihour);
          } else {
              strkey.insert(8, ihour);
          }
          if (iminute < 10) {
              strkey.insert(10, '0');
              strkey.insert(11, iminute);
          } else {
              strkey.insert(10, iminute);
          }
          if (isecond < 10) {
              strkey.insert(12, '0');
              strkey.insert(13, isecond);
          } else {
              strkey.insert(12, isecond);
          }
           return strkey.tostring();
       }
   }



    fileuploadform.java代码清单如下:

   package com.family.fileupload.web.form;
      import org.apache.struts.action.actionform;
      import org.apache.struts.upload.formfile;
      public class fileuploadform extends actionform {
          private static final long serialversionuid = 1l;
          private formfile file = null;
          public formfile getfile() {
               return file;
          }
          public void setfile(formfile file) {
               this.file = file;
          }
     }



 

     fileupload.java代码清单如下:
  

package com.family.fileupload.web.utils;
     import java.io.filenotfoundexception;
     import java.io.fileoutputstream;
     import java.io.ioexception;
     import java.io.inputstream;
     import java.io.outputstream;
     import org.apache.struts.upload.formfile;
     public class fileupload {
         private formfile file;
         private int filesize;
         private string filename;
         private string extendname;
         private string contenttype;
         public fileupload (formfile file) {
           this.file = file;
           filename = this.file.getfilename();
           filesize = this.file.getfilesize();
           contenttype = this.file.getcontenttype();
           int position = this.file.getfilename().indexof(".");
           extendname = this.file.getfilename().substring(position + 1,
this.file.getfilename().length());
      }
      public void savetofile(string filename) {
          try {
            inputstream is = this.file.getinputstream();
            int bytesread = 0;
            byte[] buffer = new byte[8912];
            outputstream os = new fileoutputstream(filename);
            while ((bytesread = is.read(buffer, 0, 8912)) != -1) {
                 os.write(buffer, 0, bytesread);
            }
            is.close();
            os.close();
           } catch (filenotfoundexception e) {
                e.printstacktrace();
           } catch (ioexception e) {
                e.printstacktrace();
           }
      }
      public string getcontenttype() {
       return contenttype;
      }
      public string getextendname() {
           return extendname;
      }
      public formfile getfile() {
           return file;
      }
      public string getfilename() {
           return filename;
      }
      public int getfilesize() {
           return filesize;
      }}

    


2.将以上文件编译后打包成 upload.jar,并发布到liferay应用的包路径下({$liferay_home}/webapps/root/web-inf/lib);


3.编写upload.jsp并复制到liferay应用的的{$liferay_home}/webapps/root\html\js\editor\ tinymce\ plugins\advimage\目录下;

upload.jsp的代码清单如下:  

     <form action="/c/portal/fileupload?method=uploadimage" 
method="post" enctype="multipart/form-data">
       <input type="file" name="file">
       <input type="submit">
     </form>




4.编写imagepath.jsp并复制到\webapps\root\html下;
代码清单如下:

 <script>   
    function insertimage() {  
      var imageurl = '<%=(string) request.getattribute("fileurl")%>';
      if (imageurl != '') {
       opener.document.all("src").value = imageurl;
       window.close();
      }
   }
  </script>



5. 在tinymce.jsp中增加如下代码

   */
   function filebrowsercallback(field_name, url, type, win) {    
     //打开文件上传窗口
     win.open("upload.jsp");  
   }




6. 在liferay的配置文件struts-config.xml或struts-config-ext.xml文件中增加如下配置:
增加formbean配置:

 <form-beans>
     ......
     <form-bean name="fileuploadform"
type="com.family.fileupload.web.form.fileuploadform" />
</form-beans>



增加action配置:

 <action-mappings>
     ……
     <action
           name="fileuploadform"
           path="/portal/fileupload"
           scope="request"
           type="com.family.fileupload.web.action.fileuploadaction"
           parameter="method">
       <forward name="file.success" path="/filepath.jsp"/>
       <forward name="image.success" path="/imagepath.jsp"/>
    </action>
</action-mappings>



   注:本实现中有关文件上传的组件与liferay集成在同一个web应用中,事实上也可以根据实际情况将文件上传的组件独立成一个单独的web应用。但是需要对tinymce.jsp代码做适当的修改。大家不妨试试:)

扫描关注微信公众号