2011-12-16 33 views
1

我的页面中有一个CKEditor。像这样如何从JSF中的html组件向服务器发送数据

<h:head> 
    <title>Facelet Title</title> 
    <script type="text/javascript" src="ckeditor/ckeditor.js"></script> 

    <script language="JavaScript"> 

     function comeBack(){ 
      document.getElementById(editorValue).value = this.value; 
     } 

    </script> 

</h:head> 

<h:body > 

    <h:form id="ckEditorForm" prependId="false" > 

     <textarea id="editor1" 
        name="editor1" 
        onblur="alert(document.getElementById(editorValue).value = this.value);"> 

     </textarea> 

     <script type="text/javascript"> 
      CKEDITOR.replace('editor1'); 
     </script> 

     <h:inputHidden value="#{editorBean.value}" id="editorValue" /> 

     <h:commandButton id="submit" 
         value="Submit" 
         action="welcome.xhtml" /> 


    </h:form> 

</h:body> 

@ManagedBean 
@ViewScoped 
public class EditorBean { 

    private String value; 

    /** Creates a new instance of EditorBean */ 
    public EditorBean() { 

    } //end of constructor 

    public String getValue() { 
     return value; 
    } 

    public void setValue(String value) { 
     this.value = value; 
     System.out.println("Content: "+value); 
     System.out.println(); 

    } 

} //end of class EditorBean 

现在我想,用户按下提交按钮,然后编辑值保存到我的豆。我发现这个论坛上的某个人暗示了这种隐藏领域技术,但它不起作用。难道我做错了什么?我怎样才能达到我想要的?

感谢

+0

也许你可以编写自己的复合JSF组件在客户端使用CKEDITOR?这里是一个关于如何编写复合组件的好教程。 http://www.mkyong.com/jsf2/composite-components-in-jsf-2-0/ – 2011-12-16 12:18:05

回答

1

你要明白,JSF是一个HTML代码生成和JavaScript有基本只对HTML DOM树,而不是JSF源代码访问。

在浏览器中打开页面,右键单击并选择查看源代码。找到生成的<h:inputHidden>组件的HTML。它看起来像这样:

<input type="hidden" id="ckEditorForm:editorValue" name="ckEditorForm:editorValue" value="" /> 

请注意id属性值。你必须使用这个值完全在JavaScript document.getElementById()

document.getElementById("ckEditorForm:editorValue"); 

顺便说一句,更容易是只使用<h:inputTextarea>代替:

<h:inputTextarea id="editor1" value="#{editorBean.value}" /> 
<script>CKEDITOR.replace("ckEditorForm:editor1");</script> 
+0

这不起作用 `它显示了普通的文本区域,代替CKEDITOR – Basit 2011-12-16 12:36:10

-1
client side: 
call this function from your component 
function send() { 
var imageData = "Anand"; 
//call remotecommand 
passToJSFManagedBean([ { 
name : 'imagedata', 
value : imageData 
} ]); 
//Prime faces component 
<p:remoteCommand name="passToJSFManagedBean" 
action="#{guestPhotoRegiCardMB.onCapture()}" process="@this" /> 

服务器端: 参考OnCapture( ){ ... String imagedata = FacesContext.getCurrentInstance()。getExternalContext()。getRequestParameterMap()。get(“imagedata”); ..... }

相关问题