Writing html in a string

久未见 提交于 2019-12-30 18:27:21

问题


I'm trying a write a couple small lines of html in my java class that gets some data from another API. I get the data in a JSON string, and would then like to display some of it on a webpage.

To create the HTML, I try:

        StringBuilder sb = new StringBuilder();
    for(int i=0;i<leads.size();i++){
        sb.append("<p>Name: "+leads.get(i).getFirstName()+" "+leads.get(i).getLastName()+"</p>");
        sb.append("<p>Email: "+leads.get(i).getEmail()+"</p>");
        sb.append("<br />");
    }
    fullLeadData = sb.toString();

But what ends up being displayed is a literal interpretation of the html tags. Is there a way that I can create this string so that the tags will stay as tags and not the escaped characters?

The java class is a managed bean, so in the html I have:

    <body>
    <div id="display">
        #{PortalView.fullLeadData}
    </div>
</body>

Where fullLeadData is the string with the html.


回答1:


Seems like you're using JSF. Try this:

<div id="display">
    <h:outputText value="#{PortalView.fullLeadData}" escape="false"/>
</div>



回答2:


You may need to replace the escape sequences. The common ones being

‘&’ (ampersand)  ‘&amp;‘
‘"’ (double quote)  ‘&quot;‘
”’ (single quote)  ‘&#039;‘
‘<’ (less than)  ‘&lt;‘
‘>’ (greater than)  ‘&gt;‘


来源:https://stackoverflow.com/questions/10402900/writing-html-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!