dyerac  
          dyerac In Java
          公告

          日歷
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567
          統(tǒng)計
          • 隨筆 - 36
          • 文章 - 10
          • 評論 - 94
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿(5)

          隨筆分類(49)

          隨筆檔案(36)

          文章分類(11)

          文章檔案(10)

          相冊

          dyerac

          搜索

          •  

          積分與排名

          • 積分 - 79161
          • 排名 - 705

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

           
          ?? FCKeditor,作為現(xiàn)在功能最強大的在線HTML編輯器,網(wǎng)上關(guān)于他的功能介紹以及基本配置已經(jīng)很多了。然而其中不少文章涉及面都比較局限。最近,筆者需要在自己項目中使用到FCKeditor,并用之于和已有的基于JSF的web應(yīng)用整合。從對FCKeditor一竅不通到成功達成整合,我從網(wǎng)上學(xué)到了不少知識,自己也積累了不少經(jīng)驗,因此,也想和大家一起分析這一過程。

          ?? 1.基本配置:

          ????? 知之為知之,不知google之。關(guān)于FCKeditor的基本配置在網(wǎng)上自有高人指點,這里我也不多耽誤大家時間。主要是談下自己當(dāng)初配置的問題:
          ??? a.基本路徑:
          ??????? 注意FCKeditor的基本路徑應(yīng)該是/(你的web應(yīng)用名稱)/(放置FCKeditor文件的文件夾名)/
          ??????? 我的目錄結(jié)構(gòu)為:
          ???????
          ??????? 因此,我的基本路徑設(shè)置為:/fck/FCKeditor/

          ???? b.文件瀏覽以及上傳路徑設(shè)置:
          ??????? 我個人的參考如下:
          ??????
          FCKConfig.LinkBrowser?=?true?;
          FCKConfig.LinkBrowserURL?=?FCKConfig.BasePath?+?'filemanager/browser/default/browser.html?Connector=connectors/jsp/connector'?;
          FCKConfig.LinkBrowserWindowWidth????=?FCKConfig.ScreenWidth?*?0.7?;????????//?70%
          FCKConfig.LinkBrowserWindowHeight????=?FCKConfig.ScreenHeight?*?0.7?;????//?70%

          FCKConfig.ImageBrowser?=?true?;
          FCKConfig.ImageBrowserURL?=?FCKConfig.BasePath?+?'filemanager/browser/default/browser.html?Type=Image
          &Connector=connectors/jsp/connector';
          FCKConfig.ImageBrowserWindowWidth??=?FCKConfig.ScreenWidth?*?0.7?;????//?70%?;
          FCKConfig.ImageBrowserWindowHeight?=?FCKConfig.ScreenHeight?*?0.7?;????//?70%?;

          FCKConfig.FlashBrowser?=?true?;
          FCKConfig.FlashBrowserURL?=?FCKConfig.BasePath?+?'filemanager/browser/default/browser.html?Type=Flash
          &Connector=connectors/jsp/connector'?;
          FCKConfig.FlashBrowserWindowWidth??=?FCKConfig.ScreenWidth?*?0.7?;????//70%?;
          FCKConfig.FlashBrowserWindowHeight?=?FCKConfig.ScreenHeight?*?0.7?;????//70%?;

          FCKConfig.LinkUpload?=?true?;
          FCKConfig.LinkUploadURL?=?FCKConfig.BasePath?+?'filemanager/upload/simpleuploader?Type=File'?;
          FCKConfig.LinkUploadAllowedExtensions????=?""?;????????????//?empty?for?all
          FCKConfig.LinkUploadDeniedExtensions????=?".(php|php3|php5|phtml|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|dll|reg|cgi)$"?;????//?empty?for?no?one

          FCKConfig.ImageUpload?=?true?;
          FCKConfig.ImageUploadURL?=FCKConfig.BasePath?+?'filemanager/upload/simpleuploader?Type=Image'?;
          FCKConfig.ImageUploadAllowedExtensions????=?".(jpg|gif|jpeg|png)$"?;????????//?empty?for?all
          FCKConfig.ImageUploadDeniedExtensions????=?""?;????????????????????????????//?empty?for?no?one

          FCKConfig.FlashUpload?=?true?;
          FCKConfig.FlashUploadURL?=?FCKConfig.BasePath?+?'filemanager/upload/simpleuploader?Type=Flash'?;
          FCKConfig.FlashUploadAllowedExtensions????=?".(swf|fla)$"?;????????//?empty?for?all
          FCKConfig.FlashUploadDeniedExtensions????=?""?;????????????????????//?empty?for?no?one


          2.與JSF整合。

          ? FCKeditor與JSF整合的最大問題,在于需要從JSF環(huán)境中相應(yīng)Bean讀取值賦予給FCKeditor,同時從FCKeditor里面讀取結(jié)果賦予給相應(yīng)的數(shù)據(jù)持有Bean。由于這一過程在傳統(tǒng)的JSF標(biāo)簽中是通過值綁定有框架自動完成,因此這里需要我們手動來實現(xiàn)這一過程。
          ? 以下要展示的demo由兩部分組成:
          ?? form.jsp顯示編輯內(nèi)容,點擊其submit按鈕跳轉(zhuǎn)至test.jsp,test.jsp調(diào)用FCKeditor對form.jsp中顯示的已有內(nèi)容進行顯示和編輯。編輯完后點擊submit,頁面將重新跳轉(zhuǎn)到form.jsp,顯示修改后的內(nèi)容。
          ? 其實,這就是一個很基本的編輯功能,在論壇和blog中都可以看到它的身影。
          ?? 而ContextBean用于持有待編輯的內(nèi)容,它的scope是session范圍。ContextServlet用于讀取FCKeditor修改后的內(nèi)容,并賦予ContextBean中。

          ??? 首先來看form.jsp
          <body>??
          ????????
          <f:view>
          ????????????
          <h:form>
          ????????????????
          <pre>
          ????????????????
          <h:outputText?value="#{td.name}"?escape="false"></h:outputText>
          ????????????????
          </pre>
          ????????????????????????????????
          <br/>
          ????????????????
          <h:commandButton?value="submit"?action="#{td.submit}"></h:commandButton>
          ????????????
          </h:form>
          ????????
          </f:view>
          ????
          </body>

          ??? 這個頁面很簡單,其中td對應(yīng)的就是ContextBean,ContextBean.name用于保存編輯內(nèi)容

          package?com.dyerac;

          public?class?ContextBean?{
          ????
          private?String?name;

          ????
          public?String?getName()?{
          ????????
          return?name;
          ????}


          ????
          public?void?setName(String?name)?{
          ????????
          this.name?=?name;
          ????}


          ????
          public?String?submit()?{
          ????????
          ????????
          return?"edit";
          ????}

          }


          下面來看test.jsp
          ?用過FCKeditor的都知道,F(xiàn)CKeditor可以通過三種方式來調(diào)用:
          ?script, jsp 標(biāo)簽以及java代碼。
          這里,為了方便從ContextBean中讀取name屬性內(nèi)容作為其初始值,我使用了第三種方法
          其中FCKeditor?fck=new?FCKeditor(request,"EditorDefault");初始化FCKeditor,第二個參數(shù)即為該FCKeditor實例的id,當(dāng)提交后FCKeditor內(nèi)的內(nèi)容將以該參數(shù)為變量名保存在request中。
          比如,這里我選擇了EditorDefault,所以日后讀取FCKeditor內(nèi)容時,可以通過以下語句:
          request.getParameter("EditorDefault")

          <body>
          ????????
          <form?action="/fck/servlet/ContextServlet"?method="post"?target="_blank">
          ????????
          <%FCKeditor?fck=new?FCKeditor(request,"EditorDefault");
          ??????????FacesContext?fcg
          =FacesContext.getCurrentInstance();
          ??????????ContextBean?td
          =(ContextBean)fcg.getApplication().getVariableResolver().resolveVariable(fcg,"td");
          ??????????fck.setBasePath(
          "/fck/FCKeditor/");
          ??????????fck.setValue(td.getName());
          ??????????fck.setHeight(
          new?Integer(600).toString());
          ??????????out.print(fck.create());
          ?????????
          %>
          ?????????
          <input?type="submit"?value="Submit">
          ????
          </body>

          ?修改后的結(jié)果以post方式提交給/fck/servlet/ContextServlet,該url對應(yīng)的即為ContextServlet。
          ContextServlet負責(zé)讀取FCKeditor里的內(nèi)容,并賦予給session中的ContextBean。doPost()方法用于實現(xiàn)該功能

          public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
          ????????????
          throws?ServletException,?IOException?{
          ????????FacesContext?fcg?
          =?getFacesContext(request,response);
          ????????ContextBean?td?
          =?(ContextBean)?fcg.getApplication()
          ????????????????.getVariableResolver().resolveVariable(fcg,?
          "td");
          ????????String?name
          =new?String(request.getParameter("EditorDefault").getBytes("ISO-8859-1"),"UTF-8");
          ????????td.setName(name);
          ????????RequestDispatcher?rd
          =getServletContext().getRequestDispatcher("/form.faces");
          ????????rd.forward(request,?response);
          ????}

          需要注意兩個問題,
          其一:FCKeditor內(nèi)的中文信息讀取是可能出現(xiàn)亂碼,需要額外的處理:
          ?? String?name=new?String(request.getParameter("EditorDefault").getBytes("ISO-8859-1"),"UTF-8");
          其二:由于servlet處于FacesContext范圍外,因此不能通過FacesContext.getCurrentInstance()來得到當(dāng)前FacesContext,因此ContextServlet定義了自己的方法用于獲取FacesContext:

          //?????You?need?an?inner?class?to?be?able?to?call?FacesContext.setCurrentInstance
          //?????since?it's?a?protected?method
          ????private?abstract?static?class?InnerFacesContext?extends?FacesContext
          ????
          {
          ??????
          protected?static?void?setFacesContextAsCurrentInstance(FacesContext?facesContext)?{
          ????????FacesContext.setCurrentInstance(facesContext);
          ??????}

          ????}


          ????
          private?FacesContext?getFacesContext(ServletRequest?request,?ServletResponse?response)?{
          ??????
          //?Try?to?get?it?first
          ??????FacesContext?facesContext?=?FacesContext.getCurrentInstance();
          ??????
          if?(facesContext?!=?null)?return?facesContext;

          ??????FacesContextFactory?contextFactory?
          =?(FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
          ??????LifecycleFactory?lifecycleFactory?
          =?(LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
          ??????Lifecycle?lifecycle?
          =?lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

          ??????
          //?Either?set?a?private?member?servletContext?=?filterConfig.getServletContext();
          ??????
          //?in?you?filter?init()?method?or?set?it?here?like?this:
          ??????
          //?ServletContext?servletContext?=?((HttpServletRequest)request).getSession().getServletContext();
          ??????
          //?Note?that?the?above?line?would?fail?if?you?are?using?any?other?protocol?than?http
          ??????ServletContext?servletContext?=?((HttpServletRequest)request).getSession().getServletContext();

          ??????
          //?Doesn't?set?this?instance?as?the?current?instance?of?FacesContext.getCurrentInstance
          ??????facesContext?=?contextFactory.getFacesContext(servletContext,?request,?response,?lifecycle);

          ??????
          //?Set?using?our?inner?class
          ??????InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);

          ??????
          //?set?a?new?viewRoot,?otherwise?context.getViewRoot?returns?null
          ??????
          //UIViewRoot?view?=?facesContext.getApplication().getViewHandler().createView(facesContext,?"yourOwnID");
          ??????
          //facesContext.setViewRoot(view);

          ??????
          return?facesContext;
          ????}
          ?

          ?? ContextServlet處理完了FCKeditor內(nèi)容后,將跳轉(zhuǎn)到form.jsp。
          這樣一個簡單的編輯功能就完成了。

          3.遺留問題:

          ?? 我在上傳文件時還是會出現(xiàn)中文亂碼的問題,按照其他人說的那樣把網(wǎng)頁的charset=utf-8改成gb2312一樣會有問題,還請各位高手賜教^_^


          另外,關(guān)于整個demo的源代碼如果大家需要,可以留言給我,我用郵箱發(fā)給您。不足之處,還請各位多多指點

          posted on 2006-10-04 20:52 dyerac in java... 閱讀(3877) 評論(32)  編輯  收藏 所屬分類: JavaEE
          評論:
          • # re: FCKeditor與JSF的整合  黑蝙蝠 Posted @ 2006-10-04 23:36
            發(fā)個給我吧 多謝
            yh10000@sina.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  dyerac in java... Posted @ 2006-10-05 00:06
            @黑蝙蝠
            發(fā)了  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  didibabawu Posted @ 2006-10-05 10:43
            也發(fā)個給我吧,謝謝!

            raywang2005@gmail.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  dsd Posted @ 2006-10-06 12:59
            麻煩發(fā)一個給我吧,先多謝啦!
            wolfhml2008@163.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  gg Posted @ 2006-10-08 10:08
            嗯,也給我一份吧,謝謝
            calmdiss@163.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  af Posted @ 2006-10-08 18:05
            xzhoujun@gmail.com
            thanks!  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  Nelson Posted @ 2006-10-09 10:29
            如果你解決了上傳文件時的中文名問題,請告知一下:nelson1983@gmail.com
            謝謝!!! ^_^  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  狂人思維 Posted @ 2006-10-09 12:42
            佩服樓主的工作態(tài)度,由于JSF的模型更新機制的限制,要集成此類的常用東東的確很不容易.

            但相信應(yīng)該還是有更好更內(nèi)聚一些的辦法來解決該問題,以下是我給您的建議:
            您可以編寫一個針對FCKEditor的JSF組件,該組件的value就用來接收FCKEditor的內(nèi)容值,另提供一個charset屬性,用于設(shè)計FCKEditor的內(nèi)容編碼.
            接下來為該組件編寫render和tag類,renderkit在encodeEnd()時負責(zé)調(diào)用FCKEditor的相關(guān)類生成編輯器HTML以及為其傳遞編輯器的文字內(nèi)容;renderkit在decode()時負責(zé)將頁面form的文本內(nèi)容域的值綁定到你的JSF組件的value內(nèi)即可.
            在decode()和encodeEnd()這兩個方法中,可以根據(jù)您的組件設(shè)置的charset進行相應(yīng)的轉(zhuǎn)換或其它的一些工作...

            這樣一來是不是就比樓主現(xiàn)在的方法在簡潔性和可重用性上要優(yōu)越一些呢.  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  dyerac in java... Posted @ 2006-10-10 13:44
            @狂人思維
            確實
            我以前也考慮過這個問題的,但是寫JSF組件真是一件體力活.....
            說實在的,我對JSF現(xiàn)在十分失望,覺得它沒有宣傳的那么強大
            您對JSF很有研究嗎?
            還望交流交流 ^_^  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  RomKK[匿名] Posted @ 2006-12-29 11:53
            給我來一份吧!romkk@163.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  michael_love911 Posted @ 2007-03-20 12:23
            我也想要,先謝謝了 ^_^

            michael_love911@21cn.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  蜘蛛 Posted @ 2007-07-09 01:44
            javazhai@gmail.com

            給我一份.兄弟  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  sz Posted @ 2007-07-20 21:45
            找了好久,麻煩給我份吧!謝謝1
            sz6554@163.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  小偉 Posted @ 2007-09-19 17:39
            俺也要一份參考!謝謝

            學(xué)習(xí)jsf時,被他極其簡單的實例所吸引,等到用上工作時,才知道這里在的井有多深啊!!

            同仁兄所整合的在線編輯器正是我近期正要準(zhǔn)備實現(xiàn)的,請先行者發(fā)一份給予參考,不盛感激。。。

            郵箱:fangbiao21@gmail.com

            希然經(jīng)過大家的不斷學(xué)習(xí)和努力,讓jsf變得不再艱難!  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  小偉 Posted @ 2007-10-11 13:42
            其上傳圖片安全性是個問題  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  baisj Posted @ 2007-12-04 18:17
            sxhybsj@yahoo.com.cn
            給我一份.兄弟  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  gembin Posted @ 2008-02-26 16:06
            gembin@gmail.com
            強烈需要源碼阿,謝謝!!!!!!!!!!!!!!!!!!!!!  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  ytwzq Posted @ 2008-03-18 11:24
            也麻煩給我一份源碼,謝謝!!!急需啊!!!!ytwzq@163.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  急了 Posted @ 2008-03-28 10:39
            如果你解決了上傳圖片出現(xiàn)亂碼的問題,麻煩告訴一下
            wd2005b@163.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  sonnychen Posted @ 2008-05-08 15:53
            也麻煩給我一份源碼,謝謝!!!急需啊!!!!sonnychen@stn.com.tw  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  Justin Posted @ 2008-05-18 10:18
            樓主,你好,麻煩把demo的源代碼發(fā)給我一份吧,我的e-mail:future_5188@tom.com,謝謝!

            如果方便的話把你的聯(lián)系方式也發(fā)到郵箱一下,以后我們可以多多交流!  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  gembin Posted @ 2008-05-28 23:05
            等到現(xiàn)在還沒發(fā)給我。。。。。。。。。。。。 :(  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  王仕軍 Posted @ 2008-07-19 16:27
            我實在沒有辦法了,把這個demo發(fā)給我吧   回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  王仕軍 Posted @ 2008-07-19 16:28
            12403367@163.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合[未登錄]  andy Posted @ 2009-07-31 17:21
            建議大家先了解JSP EL用法,這種Java腳本解決方案實在不敢恭維。。。  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  jianwenzhang Posted @ 2010-01-14 10:14
            我用了試了很久還是不行,麻煩發(fā)給我一個。jianwenzhang@yeah.net  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合[未登錄]  孟雪雪 Posted @ 2011-09-13 11:16
            發(fā)給我吧 學(xué)習(xí)學(xué)習(xí) 現(xiàn)在做項目也在用jsf 郵箱651463679@qq.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  jhoncn Posted @ 2012-08-14 15:42
            765386472@qq.com   回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合[未登錄]  jerry Posted @ 2012-12-25 17:31
            麻煩發(fā)一份給我吧!
            最近在也想整合jsf和ckeditor,網(wǎng)上有個ck-jsf-editor,樓主知道是否可用
            ?  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合[未登錄]  jerry Posted @ 2012-12-25 17:34
            忘了說郵箱了: 195308646@qq.com  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合[未登錄]  小羅 Posted @ 2013-11-29 15:49
            505724767@qq.com
            麻煩你了!  回復(fù)  更多評論   

          • # re: FCKeditor與JSF的整合  re Posted @ 2014-05-09 20:02
            776959154@qq.com
            麻煩發(fā)一份給我吧!
            麻煩你了!  回復(fù)  更多評論   

           
          Copyright © dyerac in java... Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 莱阳市| 卫辉市| 泾川县| 勃利县| 射阳县| 八宿县| 苏州市| 屯昌县| 黄山市| 四会市| 伊金霍洛旗| 南丰县| 德江县| 北辰区| 固阳县| 菏泽市| 白玉县| 兴安县| 湾仔区| 靖远县| 兰坪| 斗六市| 宁晋县| 杂多县| 剑川县| 洛南县| 广安市| 伊川县| 莆田市| 灵璧县| 历史| 新乡市| 土默特右旗| 定边县| 信阳市| 华阴市| 临海市| 汝州市| 大渡口区| 通州市| 光泽县|