在字符串中编写html [英] Writing html in a string

查看:171
本文介绍了在字符串中编写html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的java类中写一些html的小行,从另一个API获取一些数据。我得到一个JSON字符串中的数据,然后想在网页上显示一些。



要创建HTML,我尝试:

  StringBuilder sb =新的StringBuilder(); (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();

但是最终显示的是HTML标签的字面解释。有没有办法我可以创建这个字符串,以便标签将保留为标签而不是转义的字符?



java类是一个托管bean,所以在html我有:

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

其中fullLeadData是带有html的字符串。

解决方案

似乎你正在使用JSF。尝试这样:

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


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.

解决方案

Seems like you're using JSF. Try this:

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

这篇关于在字符串中编写html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆