How to stop html textarea from interpreting html entities into their characters

蓝咒 提交于 2019-12-29 09:37:09

问题


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 ® 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® 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 '&'. But I'm still wondering if there is a way to stop textareas from converting the strings they recieve.


回答1:


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;.



来源:https://stackoverflow.com/questions/3636956/how-to-stop-html-textarea-from-interpreting-html-entities-into-their-characters

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