2017-05-03 75 views
1

我想讀取和寫入我的XPages應用程序中的RTF文本字段。在XPages中顯示和編輯RTF文本字段

在我Employee類我已經定義了應包含儘可能遵循了豐富的文本字段的內容領域:

private com.ibm.xsp.http.MimeMultipart comment; 

public com.ibm.xsp.http.MimeMultipart getComment() { 
    return comment; 
} 
public void setComment(com.ibm.xsp.http.MimeMultipart comment) { 
    this.comment = comment; 
} 

現在在我的EmployeeDAO類我不知道我應該怎麼加載在Notes中的富文本內容文件並設置評論欄?

我發現下面的辦法,我認爲是不是「優雅」:

public void loadRT(Document doc){ 
     MimeMultipart strValue = null; 

     try { 
      if (doc.hasItem("Body")){ 
       if (doc.getFirstItem("Body") != null) { 
//?? not sure what the type value stands for 
        if (doc.getFirstItem("Body").getType() != 1) { 
         strValue = MimeMultipart.fromHTML(doc.getItemValueString("Body")); 
        } else { 
         RichTextItem rti = (RichTextItem) doc.getFirstItem("Body"); 
         if (rti != null) { 
          HttpServletRequest rq = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 
          String curURL = rq.getRequestURL().toString(); 
          String docid = doc.getUniversalID(); 

          String notesURL = curURL.substring(0, curURL.indexOf(rq.getContextPath()) + 1) + doc.getParentDatabase().getFilePath() + "/0/" + docid + "/" + "Body" 
            + "?OpenField"; 

          URL docURL; 
          try { 
           docURL = new URL(notesURL); 
           URLConnection uc = docURL.openConnection(); 
           uc.setRequestProperty("Cookie", rq.getHeader("Cookie")); 
           uc.connect(); 

           // do the HTTP request 
           BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8")); 

           // process the data returned 
           StringBuffer strBuf = new StringBuffer(); 
           String tmpStr = ""; 
           while ((tmpStr = in.readLine()) != null) { 
            strBuf.append(tmpStr); 
           } 

           strValue = MimeMultipart.fromHTML(strBuf.toString()); 
           employee.setComment(strValue); 


          } catch (MalformedURLException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
         } 
        } 
       } 

      } 
     } catch (NotesException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

在我的XPage我可以綁定這一個XP:爲遵循inputRichText控制:

<xp:inputRichText id="inputRichText1" 
       value="#{employeeBean.employee.comment}" style="height:400px" 
       readonly="#{employeeBean.employee.editable eq false}"> 
      </xp:inputRichText> 

我到目前爲止我還沒有如何將富文本控件調整爲其內容的大小,也沒有排除某些富文本工具欄選項。

我還沒有來到那麼遠如何保存任何調整。

我的方法是否正確?還是有更整潔,更清潔的方法?

僅供參考Notes文檔中的字段是Rich Text類型。內容是「唯一」的文字。目前使用Domino(web)表單並且使用Rich Text類型的字段。

任何幫助都非常感謝,因爲任何解釋/示例代碼似乎都很少!

+0

是否需要在傳統的Notes表單中顯示數據?還是僅僅存儲數據? –

+0

你好弗蘭克,文檔不能通過Notes表單編輯(該表單僅限於web) – Malin

+0

在xpages應用程序中,我們將RichText html作爲純文本存儲在一個字段中,因爲網頁RichText不存在。 –

回答

0

跟我說話:網上沒有RichText,只有MIME。

註釋RichText Item可以將數據存儲在「傳統」RichText或Mime條目中。它是您可以在表單的字段屬性中設置的屬性。您希望將數據存儲爲MIME,但可能會將內容仍爲「經典」RichText。區分這兩個。

對於經典,你可以使用Convert to HTML來獲得一個合適的網頁字符串。

回寫時首先刪除正文字段並使用MimeEntity(請參閱示例代碼的鏈接)重新創建它。

另外this gist提供了一個粗略的想法,你如何編碼它。

希望有幫助!

+0

謝謝!我會看看它,然後再試一次。 – Malin