textarea将html实体解释为他们的角色

textarea将html实体解释为他们的角色

本文介绍了如何阻止html textarea将html实体解释为他们的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html textarea用来显示数据库中的字符串。 textarea可以被编辑,任何改变都会被保存到数据库中。



问题是,当它接收到实体& reg;在一个字符串中它将它们转换成它们的字符,然后当文本被保存时这些字符覆盖实体。例如:数据库将返回字符串Microsoft& reg;这将显示为Microsoft®,然后以此方式保存。有没有办法强制textareas不解释实体?



后续行动:我现在的想法是,从数据库中取出字符串替换所有'&'与'& amp;'。但我仍然想知道是否有办法阻止textareas转换他们收到的字符串。 所以你想要转义HTML实体?您可以使用JSTL 或。 p>

 <%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core %> 
...
< textarea>< c:out value =$ {bean.text}/>< / textarea>

 <%@ taglib prefix =fnuri =http://java.sun.com/jsp/jstl/functions%> 
...
< textarea> $ {fn:escapeXml(bean.text)}< / textarea>

每个& & amp; ,以便例如& reg; 变成 & amp; reg; 并以这种方式将 显示为& reg;


I have an html textarea that is used to display a string from a database. The textarea can be edited and any changes are saved to the database.

The problem is that when it recieves entities &reg; in a string it converts them into their characters, and then when the text is saved the characters overwrite the entities. For example: The database would return the string Microsoft&reg; which would be displayed as Microsoft®, and then saved that way. Is there a way to force textareas to not interpret entities?

Follow up: my thought right now is to, on getting the string from the database replace all '&' with '&amp;'. But I'm still wondering if there is a way to stop textareas from converting the strings they recieve.

解决方案

So you want to escape HTML entities? You can use either JSTL <c:out> or fn:escapeXml().

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<textarea><c:out value="${bean.text}" /></textarea>

or

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<textarea>${fn:escapeXml(bean.text)}</textarea>

It will by default escape under each & to &amp; so that for example &reg; ends up to become &amp;reg; and this way it will be displayed as &reg;.

这篇关于如何阻止html textarea将html实体解释为他们的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:55