从JavaScript获取价值到Bean

时间:2017-02-27 03:42:53

标签: javascript java

我想从javascript为托管bean的属性设置值。 以下是必需的文件:

页面 sample.xhtml

       <h:body>
          <form style="width: 800px; margin: 0 auto;">
              <div class="adjoined-bottom">
                 <div class="grid-container">
    	        <div class="grid-width-100">
    	           <div id="editor">
    	              <h1>Hello world!</h1>
    		   </div>
    		</div>
    	     </div>
              </div>
              <div id="content2" style="display: none">
    	     <p>The number of <code>change</code> events: <strong><span id="changes"></span></strong>.</p>
    	  </div>
    	  <h:form>
    	     <p:commandButton value="Post" 
    			      action="#{editorController.save}" ajax="false"
    			      style="width: 50px;height: 30px;font-size: 13px"
    		              styleClass="btnClass"/>
    	  </h:form>
    	  <h:outputText id="editorcontent2" value="#{editorController.content}" escape="false"/>	
    	     <script>
    	        (function () {
    	           var changesCount = 0;
                       var editor2 = CKEDITOR.replace( 'editor', {
                       removePlugins: 'sourcearea'
    					} );
                       editor2.on( 'change', function ( ev ) {
                           changesCount++;
                           document.getElementById( 'content2' ).style.display = '';
                           document.getElementById( 'changes' ).innerHTML = changesCount.toString();
                           document.getElementById( 'editorcontent2' ).innerHTML = editor2.getData();
    					} );
    				})();
                 </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 显示了编辑器的内容,但是在点击发布按钮后,DataBase上的字段 editorContent NULL

请你解决这个问题。非常感谢。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

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

HTH