开发者

JSTL replace character in variable with HTML tag

开发者 https://www.devze.com 2023-03-27 17:49 出处:网络
I\'ve got a variable in JSTL and would like to replace all commas with <c:set var=\"colTxt\" value=\"${fn:replace(colTxt,\',\',\'<br />\')}\" />

I've got a variable in JSTL and would like to replace all commas with

<c:set var="colTxt" value="${fn:replace(colTxt,',','<br />')}" />

This however shows the error:

The value of attribute 'val开发者_如何学编程ue' associated with an element type "c:set" must not contain the '<' character

What can I do about this?


Use &lt; and &gt; instead of < and >.

When printing it using <c:out>, then ensure that you add escapeXml="false", otherwise you'll see <br /> literally showing as-is instead of being parsed as a real HTML linebreak.

However, when ${colTxt} contains user-controlled input, then disabling XML escaping might potentially create a XSS hole. You might want to solve the problem differently, e.g.

<c:forEach items="${fn:split(colTxt, ',')}" var="item">
    <c:out value="${item}" /><br />
</c:forEach>
0

精彩评论

暂无评论...
验证码 换一张
取 消