如何正确转义 JSP 标签中的三重嵌套引号

发布于 2024-07-30 04:03:58 字数 599 浏览 3 评论 0 原文

我们刚刚升级了 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}"/>'/>">

上面的标签无法编译。

We just upgraded Tomcat and the newer Tomcat doesn't like nested quotes in the tag, so we have to alternate between single and double quotes. For example,

We used to have,

<form id="search" action="<fmt:message key="search.url"/>">

Now we can change it to,

<form id="search" action="<fmt:message key='search.url'/>">

What should I do if the quotes are triply nested like this,

<form id="search" action="<fmt:message key='<c:out value="${requestScope.search_url}"/>'/>">

The above tag doesn't compile.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪淡了忧伤 2024-08-06 04:03:58

如果您不想仅为了 tomcat 升级而更新所有 jsp:s,请设置系统属性 "org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING" 为 false。

最简单的方法是编辑 catalina.sh 并将以下内容添加到 JAVA_OPTS:

-Dorg.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false  

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:

-Dorg.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false  
零崎曲识 2024-08-06 04:03:58

几种方法:

  1. 如果您不需要 XML 转义它,实际上没有必要

  2. < /code> 有一个 var 属性,它将结果存储在页面上下文中:

    > 
      <表单 id="搜索" 操作="${search_url}"> 
      

  3. 对于这种情况 < ;c:out> 是强制性的(XML 转义等等,但是我质疑消息键的 XML 转义的值),它还有一个 var 属性:

     
      > 
      <表单 id="搜索" 操作="${search_url}"> 
      

Several ways:

  1. <c:out> is actually not necessary if you don't need to XML-escape it:

    <form id="search" action="<fmt:message key='${requestScope.search_url}'/>">
    
  2. <fmt:message> has a var attribute which stores the result in page context:

    <fmt:message key="${requestScope.search_url}" var="search_url" />
    <form id="search" action="${search_url}">
    
  3. 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 a var attribute as well:

    <c:out value"${requestScope.search_url}" var="search_url" />
    <fmt:message key="${search_url}" var="search_url" />
    <form id="search" action="${search_url}">
    
城歌 2024-08-06 04:03:58

您可能早就解决了这个问题,但万一其他人遇到这个问题:

这不会编译不是因为嵌套的引号,而是因为嵌套的标签。 您不能在 fmt:message 标记的属性内使用 ac:out。 但是,您可以通过设置临时变量来使其工作:

<c:set var="foo"><c:out value="${requestScope.search_url}"/></c:set>
<form id="search" action="<fmt:message key='${foo}'/>">

此外,将示例称为“三重”嵌套引号会产生误导。 从 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:

<c:set var="foo"><c:out value="${requestScope.search_url}"/></c:set>
<form id="search" action="<fmt:message key='${foo}'/>">

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.

混浊又暗下来 2024-08-06 04:03:58

我没有尝试过这个,但在Java的其他地方,你可以转义嵌套引号,然后转义双嵌套引号的\:

<form id="search" action="<fmt:message key=\"<c:out
    value=\\\"${requestScope.search_url}\\\"/>\"/>">

编辑:由于它是一个属性,上面的方法可能不起作用,但类似的方法可能使用单引号:

<form id="search" action="<fmt:message key='<c:out
    value=\'${requestScope.search_url}\'/>'/>">

或者,使用方法调用并让它返回格式化的字符串...

I've not tried this, but elsewhere in Java you can just escape the nested quotes, then escape the \ for the double-nested quotes:

<form id="search" action="<fmt:message key=\"<c:out
    value=\\\"${requestScope.search_url}\\\"/>\"/>">

Edit: As it is an attribute, the above probably won't work, but a similar approach might work with single-quotes:

<form id="search" action="<fmt:message key='<c:out
    value=\'${requestScope.search_url}\'/>'/>">

Alternatively, use a method call and have it return the formatted String...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文