如何正确转义 JSP 标签中的三重嵌套引号
我们刚刚升级了 Tomcat,较新的 Tomcat 不喜欢标记中嵌套引号,因此我们必须在单引号和双引号之间交替。 例如,
我们以前有,
<form id="search" action="<fmt:message key="search.url"/>">
现在我们可以将其更改为,
<form id="search" action="<fmt:message key='search.url'/>">
如果引号像这样三层嵌套该怎么办,
<form id="search" action="<fmt:message key='<c:out value="${requestScope.search_url}"/>'/>">
上面的标签无法编译。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不想仅为了 tomcat 升级而更新所有 jsp:s,请设置系统属性 "org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING" 为 false。
最简单的方法是编辑 catalina.sh 并将以下内容添加到 JAVA_OPTS:
If you don't want do update all your jsp:s just for the tomcat upgrade, set the system property "org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING" to false.
Easiest way to this is by editing catalina.sh and adding the following to JAVA_OPTS:
几种方法:
:var
属性,它将结果存储在页面上下文中:对于这种情况
< ;c:out>
是强制性的(XML 转义等等,但是我质疑消息键的 XML 转义的值),它还有一个var
属性:Several ways:
<c:out>
is actually not necessary if you don't need to XML-escape it:<fmt:message>
has avar
attribute which stores the result in page context:For the case
<c:out>
is mandatory (XML escaping and so on, I however question the value of XML escaping for message keys), it has avar
attribute as well:您可能早就解决了这个问题,但万一其他人遇到这个问题:
这不会编译不是因为嵌套的引号,而是因为嵌套的标签。 您不能在 fmt:message 标记的属性内使用 ac:out。 但是,您可以通过设置临时变量来使其工作:
此外,将示例称为“三重”嵌套引号会产生误导。 从 jsp 引擎的角度来看,表单标记的 action 属性值周围的双引号字符的行为与引号不同。 ${...} EL 表达式之外的任何内容,或者带有 known 前缀的 known jsp 标记之外的任何内容都将被视为任意字节。
You've probably long since solved this problem, but in case anyone else runs across this:
That doesn't compile not because of the nested quotes, but because of the nested tags. You can't use a c:out inside the attribute of the fmt:message tag. However, you can get it to work by setting a temporary variable:
Also, calling your example "triply" nested quotes is misleading. The double quote characters surrounding the value of the action attribute of your form tag do NOT behave like quotes from the point of view of the jsp engine. Anything outside of a ${...} EL expression, or outside of a known jsp tag with a known prefix is treated as arbitrary bytes.
我没有尝试过这个,但在Java的其他地方,你可以转义嵌套引号,然后转义双嵌套引号的\:
编辑:由于它是一个属性,上面的方法可能不起作用,但类似的方法可能使用单引号:
或者,使用方法调用并让它返回格式化的字符串...
I've not tried this, but elsewhere in Java you can just escape the nested quotes, then escape the \ for the double-nested quotes:
Edit: As it is an attribute, the above probably won't work, but a similar approach might work with single-quotes:
Alternatively, use a method call and have it return the formatted String...