2017-02-27 78 views
1

我想從javascript中設置值爲託管bean的屬性。 下面是所需的文件:從JavaScript中獲取價值到Bean

sample.xhtml

 <h:body> 
 
      <form style="width: 800px; margin: 0 auto;"> 
 
       <div class="adjoined-bottom"> 
 
       <div class="grid-container"> 
 
    \t   <div class="grid-width-100"> 
 
    \t   <div id="editor"> 
 
    \t    <h1>Hello world!</h1> 
 
    \t \t </div> 
 
    \t \t </div> 
 
    \t  </div> 
 
       </div> 
 
       <div id="content2" style="display: none"> 
 
    \t  <p>The number of <code>change</code> events: <strong><span id="changes"></span></strong>.</p> 
 
    \t </div> 
 
    \t <h:form> 
 
    \t  <p:commandButton value="Post" 
 
    \t \t \t  action="#{editorController.save}" ajax="false" 
 
    \t \t \t  style="width: 50px;height: 30px;font-size: 13px" 
 
    \t \t    styleClass="btnClass"/> 
 
    \t </h:form> 
 
    \t <h:outputText id="editorcontent2" value="#{editorController.content}" escape="false"/> \t 
 
    \t  <script> 
 
    \t   (function() { 
 
    \t   var changesCount = 0; 
 
         var editor2 = CKEDITOR.replace('editor', { 
 
         removePlugins: 'sourcearea' 
 
    \t \t \t \t \t }); 
 
         editor2.on('change', function (ev) { 
 
          changesCount++; 
 
          document.getElementById('content2').style.display = ''; 
 
          document.getElementById('changes').innerHTML = changesCount.toString(); 
 
          document.getElementById('editorcontent2').innerHTML = editor2.getData(); 
 
    \t \t \t \t \t }); 
 
    \t \t \t \t })(); 
 
       </script> 
 
       <script> 
 
        initSample(); 
 
       </script> 
 
      </form> 
 
    </h:body>

的方法節省託管bean EditorController.java

public void save() 
 
{ 
 
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-configuration.xml"); 
 
    eDao = ctx.getBean(FacebookerDao.class); 
 
    editor.setEditorContent(content); 
 
    eDao.saveEditor(editor); 
 
}

成功的H:的outputText顯示編輯的內容,但是點擊按鈕後,現場editorContent數據庫後NULL

你有什麼想法解決這個問題。非常感謝。

回答

1

你可以試試這個:

<div id="content2" style="display: none"> 
 
\t \t \t \t <p>The number of <code>change</code> events: <strong><span id="changes"></span></strong>.</p> 
 
\t \t \t </div> 
 
\t \t \t <h:outputText id="editorcontent2" value="#{editorController.content}" escape="false"/> \t 
 
\t \t \t <h:form id="formId"> 
 
\t \t \t  <h:inputHidden id="x" value="#{editorController.content}" escape="false"/> 
 
\t \t \t  <h:commandButton value="submit" onclick="getEditorData()" action="#{editorController.save}" /> 
 
\t \t \t </h:form> 
 
\t \t 
 
\t \t 
 
\t \t \t <script> 
 
\t \t \t \t (function getEditorData() { 
 
\t \t \t \t \t var changesCount = 0; 
 
\t \t \t \t \t var editor2 = CKEDITOR.replace('editor', { 
 
\t \t \t \t \t \t removePlugins: 'sourcearea' 
 
\t \t \t \t \t \t \t 
 
\t \t 
 
\t \t \t \t \t }); 
 
\t \t \t \t \t 
 
\t \t \t \t \t editor2.on('change', function (ev) { 
 
\t \t \t \t \t \t changesCount++; 
 
\t \t \t \t \t \t document.getElementById('content2').style.display = ''; 
 
\t \t \t \t \t \t document.getElementById('changes').innerHTML = changesCount.toString(); 
 
\t \t \t \t \t \t document.getElementById('editorcontent2').innerHTML = editor2.getData(); 
 
\t \t \t \t \t \t document.getElementById("formId:x").value = editor2.getData(); 
 
\t \t \t \t \t }); 
 
\t \t \t \t \t 
 
\t \t \t \t })(); 
 
\t \t \t </script>

HTH

+0

謝謝@Saria。它也可以工作。 – Elisabeth

+0

不客氣。 – saria