JSTL函数来替换字符串中的引号字符? [英] JSTL function to replace quote chars inside a string?
本文介绍了JSTL函数来替换字符串中的引号字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在字符串值内用\序列替换引号字符的最简单方法是什么?
What is the simplest way to replace quote characters with \" sequence inside string values?
推荐答案
That'll be the fn:replace()
function.
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
${fn:replace(foo, '"', '\\"')}
不相关与具体问题无关,这是经常发生的要求,以防止在将用户控制的输入重新显示为HTML属性时防止HTML格式错误. 通常,您应该使用 fn:escapeXml()
代替.例如
Unrelated to the concrete question, this is an often recurring requirement in order to prevent malformed HTML when redisplaying user controlled input as a HTML attribute. Normally, you should use <c:out>
or fn:escapeXml()
for this instead. E.g.
<input name="foo" value="<c:out value="${param.foo}" />" />
<input name="foo" value="${fn:escapeXml(param.foo)}" />
它不仅考虑了引号,还考虑了所有其他XML特殊字符,例如<
,>
,&
等.
It not only takes quotes into account, but also all other XML special characters like <
, >
, &
, etc.
这篇关于JSTL函数来替换字符串中的引号字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文