JSTL转义特殊字符 [英] JSTL escaping special characters

查看:186
本文介绍了JSTL转义特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个奇怪的问题与特殊字符。
在JSP中,我使用字段名作为id,名称可以是任何类似的东西,如

  id =& (OR)
id =aaa& amp; bbb

我没有任何其他选项可以使用除名字之外的ID,这是我从后端获得的唯一的东西。



所以,是否有任何逻辑删除使用JSTL的所有特殊字符。
在目前的情况下,在JS中,我将使用ID进行一些操作。



请建议,提前感谢...

解决方案

JSTL提供了两种转义HTML特殊字符的方法:

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

  $ {fn:escapeXml(myName)} 

都将特殊字符转换为各自的HTML实体:( 成为& lt; & 成为& amp; ...)。



请注意,ID必须编码HTML,但不在JavaScript中。


I have this weird issue with special characters. In JSP, I am using field name as id and the name can be anything like

id="&amp;lt;1 and &amp;&gt;2" (OR)
id="aaa &amp; bbb"

I don't have any other option to use ID's other than names, that what the only thing I get from backend.

So, Is there any logic to remove all the special characters using JSTL. With the present scenario, In JS I will do some operations with the ID. this is causing many issues for each kind of browser.

Please suggest, Thanks in advance...

解决方案

The JSTL provides two means of escaping HTML special chars :

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

and

${fn:escapeXml(myName)}

Both wil transform the special chars into their respective HTML entities : (< becomes &lt;, & become &amp;...).

Note that the IDs must be encoded in HTML, but not in JavaScript.

这篇关于JSTL转义特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆