2014-01-16 36 views
1

爲了實現圖像瀏覽器&註釋器,我將Primefaces/JSF與純JavaScript工具結合使用。圖像查看器建立在OpenLayers framework上。jsf。如何從jsf形式發佈後臺bean對象到javascript

當用戶在畫布上註釋(繪製形狀)時,會創建一個JSON對象,並將Save操作傳遞給後端Bean。 Back bean檢索對象(反序列化)並將其存儲到文件中。

下面是相關的代碼:

的OpenLayers的JavaScript(圖像viewer.js):

function initialiseMap(){' 
    ... 
    map = new OpenLayers.Map(imageEditorID, options); 
    imageLayer = new OpenLayers.Layer.TMS(imgURL, "", { 
    ... 
    }); 
    map.addLayer(imageLayer); 
    var vlayer = new OpenLayers.Layer.Vector("Editable"); 
    map.addLayer(vlayer); 
    //draw controls and shape tools 
    ... 
    //then define save action 
    var save = new OpenLayers.Control.Button({ 
     ... 
     var GEOJSON_PARSER = new OpenLayers.Format.GeoJSON();   
     var vectorLayerAsJson = GEOJSON_PARSER.write(vlayer.features); 
     //and finally post to server layer with drawn shapes 
     sendJSONToServer([{name:'param', value:vectorLayerAsJson}]); 
     ... 

上述圖像查看器/映射工具,經由p加載:primefaces的outputPanel組分和使用sendJSONToServer remoteCommand得到JSON層:

<h:head> 
    <script src="#{facesContext.externalContext.requestContextPath}/js/image-viewer.js" /> 
    ... 
    <h:body> 
     <h:form id="imageEditor"> 
      <p:fieldset legend="Viewer"> 
       ... 
       // inoutHidden does not have on* events? how am i going to post to image-viewer.js? 
       <h:inputHidden value="#{imageAnnotations.fetchJsonString()}" /> 
       ... 
       <p:outputPanel layout="block" styleClass="imageEditorImagePanel" /> 
       <p:remoteCommand immediate="true" name="sendJSONToServer" action="#{imageAnnotations.actionOnString}" /> 
      </p:fieldset> 
     .... 

最後在backbean JSON對象被取出和(實現是原始)存儲在一個文件:

@ManagedBean(name="imageAnnotations") 
public class ImageAnnotations { 

    //actionOnString fetches and saves the JSON string - this is a raw impementation 
    public String actionOnString() { 
     //Do the job and get and save JSON string 
    } 

    public String fetchJsonString(){ 
     //Do the job and get JSON string 
     return jsonString; 
    } 
}     

的問題是我應該如何去使用JSF或primefaces元素,藉此讓該imageAnnotations.fetchJsonString()值從JS中獲取?

回答

1

即使我不能給出所有的答案,你對我的hiddenInput的填充應儘量設法如下:

@ManagedBean(name="imageAnnotations") 
public class ImageAnnotations { 

    private String jsonString; 

    public void anyMethodFillingOrInitializingTheJSONString() { 
     this.jsonString = resultOfYourWork(); 
    } 

    public String getJsonString(){ 
     return this.jsonString(); 
    } 

    public void setJsonString(String item) { 
     this.jsonString = item; 
    } 
} 

當你重新加載這個隱藏的輸入字段,只是一定要觸發的JavaScript解析字符串並更新您的客戶端模型。這可以通過您可以使用Primefaces按鈕連接的on * - 事件來完成。

夥計們,有人可以幫助其他部分?