【问题标题】:how to get value of the tinymce in jsf backing bean如何在 jsf backing bean 中获取 tinymce 的值
【发布时间】:2012-04-16 07:18:32
【问题描述】:

我在 JSF 中使用 tinymce 编辑器,但我不确定如何在我的 Backing Bean 中获取此值。请如果有人之前使用过这个,请帮助我这样做。

       <script src="resources/tiny_mce/tiny_mce.js" type="text/javascript"></script>
<script type="text/javascript">
    tinyMCE.init({
   // General options
    mode : "textareas",
    theme : "advanced",
    plugins :  "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

   // Theme options
   theme_advanced_buttons1 :"bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
   theme_advanced_buttons2 : "cut,copy,paste,pastetext,|,bullist,numlist,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,preview,|,forecolor,backcolor",
   theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,styleprops,spellchecker,|,insertfile,insertimage",
   theme_advanced_toolbar_location : "top",
   theme_advanced_toolbar_align : "left",
   theme_advanced_statusbar_location : "bottom",
   theme_advanced_resizing : true,

   // Skin options
   skin : "o2k7",
   skin_variant : "silver",

  content_css : "css/example.css",

 });

   <ice:inputTextarea id="content" value="${bean.passage}" partialSubmit="true"/>   

【问题讨论】:

  • 在这方面帮不了你,但 Primefaces 有一个 JSF 编辑器组件,你不必编写任何 JavaScript 代码。使用非常简单,见here
  • 嗨 Nikita ,我真的很抱歉,我不确定如何接受答案,如果我的问题得到解决,我只是添加评论说谢谢。请让我知道如何接受答案,我试过但不知道...

标签: jsf tinymce


【解决方案1】:

value="${bean.passage}" 替换为value="#{bean.passage}"${} 不能调用 setter 方法,但 #{} 可以。

【讨论】:

  • 即使更改后我也有同样的问题。 :(
  • JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "tinyMCE.get('content').getContent();");我也试过这个,但如果 textarea 在面板选项卡内,可悲的部分它不起作用。
【解决方案2】:

通过选择器将 tinyMCE 连接到您的 jsf 组件

在以下示例中,您将加载一个自己的 JavaScript 文件“loadhtmleditor.js”,其中包含函数“loadMyEditor()”。这个函数在body onload中被调用。 在这个 javascript 函数中,您通过 selector 而不是 mode 将 tinyMCE 连接到您的 jsf 组件。在选择器参数中,您编写要连接的组件的 html-id。请注意,在此参数中,由 jsf 生成的 html-id 中的 ':' 分隔符必须用两个反斜杠屏蔽!在此示例中:选择器:“#myForm\\:myHtmlText”

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <script src="resources/tiny_mce/tiny_mce.js" type="text/javascript"></script>
    <script src="resources/loadhtmleditor.js" type="text/javascript"></script>
</h:head>
<h:body onload="loadMyEditor();">
    <h:form id="myForm">
        <h:inputTextarea id="myHtmlText" value="#{bean.htmlText}">
        </h:inputTextarea>
    </h:form>
</h:body>
</html>

加载htmleditor.js:

function loadMyEditor()
{
    tinyMCE.init({
           // General options
            selector : "#myForm\\:myHtmlText",
            theme : "advanced",
            plugins :  "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
           // Theme options
           theme_advanced_buttons1 :"bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
           theme_advanced_buttons2 : "cut,copy,paste,pastetext,|,bullist,numlist,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,preview,|,forecolor,backcolor",
           theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,styleprops,spellchecker,|,insertfile,insertimage",
           theme_advanced_toolbar_location : "top",
           theme_advanced_toolbar_align : "left",
           theme_advanced_statusbar_location : "bottom",
           theme_advanced_resizing : true,
           // Skin options
           skin : "o2k7",
           skin_variant : "silver",
          content_css : "css/example.css",
         });
}

这应该足以将您的 jsf bean 绑定到 tinyMCE!

补充: 如果您想在用户更改 html 内容时通过 ajax 做出反应,您可以像这样扩展 tinyMCE 编辑器的创建:

加载htmleditor.js:

function loadMyEditor()
{
    tinyMCE.init({
           // General options
            selector : "#myForm\\:myHtmlText",
            theme : "advanced",
            plugins :  "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
           // Theme options
           theme_advanced_buttons1 :"bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
           theme_advanced_buttons2 : "cut,copy,paste,pastetext,|,bullist,numlist,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,preview,|,forecolor,backcolor",
           theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,styleprops,spellchecker,|,insertfile,insertimage",
           theme_advanced_toolbar_location : "top",
           theme_advanced_toolbar_align : "left",
           theme_advanced_statusbar_location : "bottom",
           theme_advanced_resizing : true,
           // Skin options
           skin : "o2k7",
           skin_variant : "silver",

           init_instance_callback: function (editor)
           {
               editor.on('Change', function(e){myFunctionEditorChanged(editor);});
               editor.on('Paste', function(e){myFunctionEditorPasted(editor);});
           },
           content_css : "css/example.css"
         });
}


function myFunctionEditorChanged(editor)
{
    tinyMCE.triggerSave(); // to be shure, that the changed content is in your jsf-component...
    // do something to trigger an ajax request handeling the changed html content. 
}


function myFunctionEditorPasted(editor)
{
    tinyMCE.triggerSave(); // to be shure, that the changed content is in your jsf-component...
    // do something to trigger an ajax request handeling the changed html content. 
}

通过这种方式,您可以对用户通过键盘或粘贴内容在 html 编辑器中输入的所有更改做出反应。当用户通过 tinyMCE 的菜单更改 html 内容时,我没有找到获取通知的方法。 (例如当用户将某些单词更改为粗体时......)

【讨论】:

    猜你喜欢
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2014-08-11
    • 1970-01-01
    • 2011-02-02
    • 2012-07-02
    • 2015-12-06
    相关资源
    最近更新 更多