【问题标题】:Rich Text Field - Format Mime Part - Get HTML Content富文本字段 - 格式化 Mime 部分 - 获取 HTML 内容
【发布时间】:2013-12-05 13:39:35
【问题描述】:

我有一个包含富文本字段的 XPage。在这个富文本字段中是一些文本,用 HTML 格式化。只要我不做任何改动,使用获取内容是很容易的

docMail.getItemValue("dBody")[0].toString() 

文档的内容是这样的:

一旦我更改了文本的任何内容,内容将保存为 MIME,我的代码不再工作,因为当我想获取 dbody 的值时,它是空的。内容如下:

所以现在是 MIME 部分。但是如何获取 HTML 代码?我只想要 HTML 代码并通过邮件发送。

【问题讨论】:

  • 如果docMail 指的是文档数据源(不是“后端”文档),请调用getValue 而不是getItemValue。否则,使用问题 Per included 中提到的包装函数将文档作为数据源...并然后调用getValue 而不是getItemValue。 :)

标签: html xpages


【解决方案1】:

在您的情况下:如果您发送整个文档 (doc.send),它应该在您将字段从 dBody 重命名为 Body 的那一刻起作用。如果您想在后端工作,请使用 doc.getMimePart 进入 body 字段。或者使用 Per 的包装器。

【讨论】:

    【解决方案2】:

    谢谢大家,但我一直在研究一个似乎也能完成这项工作的解决方案(基于XSnippet emailBean by Tony McGuckin。这是代码:

    得到一个包含此代码的新 Bean:

    package de.esg.weiterbildung;
    import lotus.domino.Database;
    import lotus.domino.NotesException;
    import lotus.domino.Session;
    import com.ibm.xsp.model.domino.wrapped.DominoDocument;
    import com.ibm.xsp.model.domino.wrapped.DominoRichTextItem;
    import com.ibm.xsp.model.domino.wrapped.DominoDocument.AttachmentValueHolder;
    import de.sit.xpagesutils.*;
    
    public class HTMLHelper {
        private boolean debugMode = true;
        private String fieldName="dBody";
        private DominoDocument document;
    
        public String getFieldName(){
            return this.fieldName;
        }
    
        public void setFieldName(final String fieldName){
            this.fieldName = fieldName;
        }   
    
        public boolean isDebugMode(){
            return this.debugMode;
        }
    
        public void setDebugMode(final boolean debugMode){
            this.debugMode = debugMode;
        }
        public DominoDocument getDocument(){
            return this.document;
        }
    
        public void setDocument(final DominoDocument document){
            this.document = document;
        }
    
        public String getBodyHTML(String unid)throws NotesException{
            String back ="";
    
            if(null != document){
                if(this.isDebugMode()){
                    System.out.println("Started getBodyHTML()");
                }
                final DominoRichTextItem drti = document.getRichTextItem(fieldName);
                if(null != drti){
                    try {
                        String html = drti.getHTML();
                        if(this.isDebugMode()){
                            System.out.println("Completed getBodyHTML() : " + html);
                        }
                        return html;
                    } catch (Exception e) {
                        if(this.isDebugMode()){
                            System.out.println("Failed getBodyHTML() : " + e.getMessage());
                        }
                    }
                }
            }
            return back;
        }
    
    }
    

    现在我通过调用以下代码来获取正文:

    helper.setDocument(docMail);
    helper.setFieldName("dBody");
    var htmlBody:String = helper.getBodyHTML(unid);
    

    到目前为止有效!

    【讨论】:

      【解决方案3】:

      这是我用来访问具有 HTML 内容的字段的方法,但可以存储为纯文本或富文本字段。我确实将富文本字段设置为将其内容存储为 HTML 和 MIME。

      String getRichTextField(Document document, String fieldName) {
        MIMEEntity me;
        try {
          me = document.getMIMEEntity(fieldName);
          String text;
          if (me != null) {
            text = me.getContentAsText();
          } else {
            @SuppressWarnings("unchecked")
            Vector<String> values = document.getItemValue(fieldName);
            text = join(values, "\n"); // join is a utility method that joins a vector of strings using the given delimiter
          }
          return text;
        } catch (NotesException e) {
          System.err.println("Error accessing field '" + fieldName + "'.");
          printStackTrace();
          return "error accessing field";
        }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-19
        • 1970-01-01
        • 2011-12-19
        • 2021-01-23
        • 2014-05-26
        • 2019-05-16
        • 1970-01-01
        相关资源
        最近更新 更多