在XPage中显示和编辑RTF字段

时间:2017-05-03 18:06:37

标签: java xpages richtext

我想读取和写入XPage应用程序中的Rich Text字段。

在我的Employee类中,我定义了一个字段,该字段应包含Rich Text字段的内容,如下所示:

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文档中加载Rich Text内容并设置注释字段?

我发现以下方式,我认为不是“优雅”:

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类型的字段。

任何帮助都非常受欢迎,因为任何解释/示例代码似乎都很稀缺!

1 个答案:

答案 0 :(得分:0)

跟我说:网上没有RichText,只有MIME。

Notes RichText项可以将数据存储在&#34; classic&#34; RichText或Mime条目。它是您可以在表单上的字段属性中设置的属性。您希望将数据存储为MIME,但可能内容仍然是&#34; classic&#34;富文本。区分两者。

对于classic,您可以使用Convert to HTML来获取适合Web的字符串。

写回时,首先删除Body字段并使用MimeEntity重新创建它(请参阅示例代码链接)。

同样this gist提供了如何编码的简单概念。

希望有所帮助!