${} template literal (ES2015) conflict with JSP Expression Language syntax
Asked Answered
J

2

16

${} is being used by both JSP and JS, so what's happening is that the ${} in the JS template literals are being interpreted and removed before being compiled into servlets.

Is there a way I can tell Java to ignore ${} without completely turning the feature off with isELIgnored attribute?

const subject = 'world';
let greet = `hello ${subject}!` 

turns into the following in the browser

const subject = 'world';
let greet = `hello !` 

Here's the best I've come up with, really not digging how ugly it is though:

<c:out value="var body = `pq_country=${country}&pq_population=${population}`;" 
       escapeXml='false'/>
Julianajuliane answered 30/8, 2016 at 13:46 Comment(4)
Just out of curiosity, why are you using back-ticks?Heterosporous
ES6 only substitutes these values when you use backticks as the delimiter. Have you tried using simple apostrophes?Treasure
Just write JS code in its own JS file instead of mixing with HTML source. That has been the "best practice" for ages.Italia
It is different to write JS in its own file for "best practice" and different for writing because you don't have any choice. Too bad that this conflict cannot be solved in a better way.Door
L
6

You have to move the JS code to the function inside the external file or script tag, it would be the best way to solve conflict of jsp with JS syntax.

Locular answered 11/9, 2016 at 14:18 Comment(0)
L
20

You can put a backslash in front of the ${} so JSP ignores it (found out through this article by David Ford).

const subject = 'world';
let greet = `hello \${subject}!`
List answered 17/11, 2020 at 21:17 Comment(0)
L
6

You have to move the JS code to the function inside the external file or script tag, it would be the best way to solve conflict of jsp with JS syntax.

Locular answered 11/9, 2016 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.