无法在JSP页面中正确显示特殊字符 [英] Not able to display special characters properly in a JSP page
问题描述
我需要在表TD中显示以下特殊字符(不包括<符号和正斜杠之间的反斜杠)
I need to display following special characters in a table TD (exclude the back slash in between < symbol and forward slash),
1) '/<\/@/test#/$/&/)'
但是,浏览器只能显示'/ 也就是说,数据正在被截断.
But, the browser is able to display only '/ i.e, data is getting truncated.
仅当<和/字符连续出现.
The above issue occurs only when < and / chars occur contiguously.
也就是说,由于<之后没有连续的正斜杠/,因此可以进行后续操作.符号
That is, following works as there is no succeeding forward slash / after the < symbol
2) '/<@/test#/$/&/)'
我正在使用Java代码来转义其他特殊字符. 我能够成功替换<带有&"号;但仅当<和/一起出现.
I am using java code to escape the other special chars. I am able to successfully replace < with ampersand lt; but how to replace a forward slash / only when < and / appear together.
请帮助我
对不起,即使在此网站中,您也看不到我需要实现的目标,因此在第1点中,请排除反斜杠(我需要显示时不带反斜杠,以供您理解.)我是新用户,因此无法发布图片.
Sorry, even in this website you cannot see what i need to achieve, hence in Point 1 exclude the back slash (I need to display without the backslash, for ur understanding i have put it). I cannot post images as i am a new user.
推荐答案
Web浏览器默认将JSP发送的所有内容都视为HTML. <
指示HTML标记的开始,因此Web浏览器将对其进行解析(并最终由于语法错误而失败).
Everthing which get sent by JSP is by default treated as HTML by the webbrowser. The <
indicates start of a HTML tag and thus the webbrowser will parse it as such (and eventually fail due to a syntax error).
您想转义诸如<
,>
,&
和"
之类的HTML特殊字符.
You want to escape those HTML special characters like <
, >
, &
and "
.
< will be displayed as <
> will be displayed as >
& will be displayed as &
" will be displayed as "
如果它是动态文本,则最好使用 JSTL <c:out>
标签.
If it is dynamic text, this is best to be done with JSTL <c:out>
tag.
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:out value="${bean.text}" />
如果要设置HTML属性,则 JSTL fn:escapeXml()
函数更好.
Of if you want to set a HTML attribute, the JSTL fn:escapeXml()
function is nicer.
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<input type="text" value="${fn:escapeXml(bean.text)}" />
这篇关于无法在JSP页面中正确显示特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!