我想要读取和写入 XPage 应用程序中的富文本字段。
在我的 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 中,我可以将其绑定(bind)到 xp:inputRichText 控件,如下所示:
<xp:inputRichText id="inputRichText1"
value="#{employeeBean.employee.comment}" style="height:400px"
readonly="#{employeeBean.employee.editable eq false}">
</xp:inputRichText>
到目前为止,我还没有了解如何将富文本控件调整为内容的大小,也没有排除一些富文本工具栏选项。
我还没有走到这一步如何保存任何调整。
我的方法正确吗?或者有更简洁的方法吗?
仅供引用,Notes 文档上的字段是富文本类型。然而,内容“只是”文本。目前使用 Domino(Web)表单和富文本类型的字段。
非常感谢任何帮助,因为任何解释/示例代码似乎都很稀缺!
最佳答案
跟我说话:网络上没有 RichText,只有 MIME。
Notes RichText 项目可以将数据存储在“经典”RichText 中或作为 Mime 条目。您可以在表单的字段属性中设置该属性。您想要将数据存储为 MIME,但内容可能仍然是“经典”RichText。区分两者。
对于经典,您可以使用 Convert to HTML获取适合网络的字符串。
回写时,您首先删除正文字段,然后使用 MimeEntity 重新创建它。 (请参阅示例代码的链接)。
还有this gist提供了如何编码的粗略想法。
希望有帮助!
关于java - 在 XPage 中显示和编辑富文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767145/