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.
The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include: Carriage return and newline: "\r" and "\n" Backslash: "\\"
You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.
If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.
Double quotes characters can be escaped with backslash( \ ) in java.
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
Several ways:
<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}'/>">
<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}">
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}">
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With