gpt4 book ai didi

java - 在 p :dialog 中使用 JSF 解析 HTML

转载 作者:行者123 更新时间:2023-12-01 13:40:16 25 4
gpt4 key购买 nike

您好,我正在 Web 应用程序中工作,例如带有 JSF 和 javamail 的 GMAIL,我使用 p:dialog 来预览电子邮件:https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-prn1/1522023_568308536589120_1043722853_n.jpg

这是VUE的代码:

<h:form id="form">  

<p:growl id="msgs" showDetail="true" />

<p:dataTable id="cars" var="message" value="#{mailservice.emails}" >
<p:ajax event="rowSelect" update=":form:display" oncomplete="carDialog.show()" />
<p:column headerText="From" style="width:24%">
<h:outputText value="#{message.from}" />
</p:column>

<p:column headerText="Subject" style="width:24%">
<h:outputText value="#{message.subject}" />
</p:column>



<p:column style="width:4%">
<p:commandButton id="selectButton" update=":form:display" oncomplete="carDialog.show()" icon="ui-icon-search" title="View">
<f:setPropertyActionListener value="#{message}" target="#{mailservice.selectedMessage}" />
</p:commandButton>
</p:column>

</p:dataTable>

<p:dialog header="Car Detail" widgetVar="carDialog" resizable="false" id="carDlg"
showEffect="fade" hideEffect="explode" modal="true">

<h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">

<h:outputText value="Meassage :" />
<h:outputText value="#{mailservice.selectedMessage.text}" style="font-weight:bold"/>

</h:panelGrid>

</p:dialog>

</h:form>

这是托管 bean 的代码,用于获取 GMAIL 电子邮件并将其存储在 ArrayList 中:

public  ArrayList<Email> getEmails()
{ ArrayList<Email> emails=new ArrayList<Email>();
try {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");

Session session = Session.getDefaultInstance(props, null);

session.setDebug(true);
Store store = session.getStore("imaps");
System.out.println(store.getClass());
store.connect("imap.gmail.com", Util.getEmail(), Util.getEmailPassword());
System.out.println(store);

Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
Message messages[] = inbox.getMessages();
int nb = messages.length;
int max;
if(nb>=3)
max=3;
else
max=nb;
for(int i = nb-1 ;i>=nb-max;i--) {
Message message = messages[i];
emails.add(new Email(message.getFrom()[0].toString(), message.getSubject(), message.getContent().toString()));
}
inbox.close(true);
} catch (Exception ex) {
Logger.getLogger(MailService.class.getName()).log(Level.SEVERE, null, ex);
}
return emails;
}

问题是 html 电子邮件显示了电子邮件的 html 代码,如下所示:

https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-prn1/1522023_568308536589120_1043722853_n.jpg

最佳答案

这是正常行为,h:outputText 默认情况下会转义 HTML 标记。

你必须更换

<h:outputText value="#{mailservice.selectedMessage.text}" style="font-weight:bold"/>

<h:outputText value="#{mailservice.selectedMessage.text}" style="font-weight:bold" escape="false" />

此时,您将获得无效的 HTML,因为包含的内容中存在 html 标记。避免这种情况的一种方法是添加 iframe

<iframe id="mailContent" src="about:blank"></iframe>
<script type="text/javascript">
var doc = document.getElementById("mailContent").contentWindow.document;
doc.open();
doc.write('<h:outputText value="#{mailservice.selectedMessage.text}" escape="false" />');
doc.close();
</script>

iframe 的信用:Specifying content of an iframe instead of the src to a page

更多信息:

关于java - 在 p :dialog 中使用 JSF 解析 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com