带有 HTML 的 JSP 输出字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9390262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:41:50  来源:igfitidea点击:

JSP output string with HTML?

htmlstringjsp

提问by Navdroid

I would like to output a string in a JSP page. The string contains HTML tag. How do I display the HTML version of the string in JSP?

我想在 JSP 页面中输出一个字符串。该字符串包含 HTML 标记。如何在 JSP 中显示字符串的 HTML 版本?

e.g.

例如

            `String str = "<b><u>bold and underlined</u></b>"`;

In JSP, I use <%=str%>

在 JSP 中,我使用 <%=str%>

Instead of displaying the HTML version of the string (with bold and underlining of the text), the above string is displayed. Can you help?

不显示字符串的 HTML 版本(文本带有粗体和下划线),而是显示上述字符串。你能帮我吗?

I also tried

我也试过

                   <% out.print(str); %>

But didnt worked for me.

但没有为我工作。

回答by Harry Joy

Better to use JSTL, something like:

最好使用 JSTL,例如:

<c:out value="${str}" escapeXml="false"/>

If stris coming in request then

如果str是在请求然后

<c:out value="${param.str}" escapeXml="false"/>

Here escapeXml="false"will instruct that html/xml tags should be evaluated and not escaped.

HereescapeXml="false"将指示应该评估 html/xml 标签而不是转义。

回答by Rangana Sampath

don't know weather this helps..

不知道天气这有帮助..

By entering the string in the following manner allows you to show the code in a text area...

通过以下列方式输入字符串,您可以在文本区域中显示代码...

 String str = "<textarea><b><u>bold and underlined</u></b></textarea>";