Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

jsp - How to pass an EL variable to javascript

I have a variable ${bean.name} how can i pass it to a javascript var? I've tried var name = "${bean.name}" and var name = ${bean.name} but it does not work.

my idea is to put it in a hidden input like in a hidden <input id="test" type="text" value"${bean.name}">

var name = document.getElementById("test").value;

this doen't work either var name becomes the string "${bean.name}"

note. i can't use jstl See Question&Answers more detail:os


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can't have JSTL evaluated in .js files. Only in .jsp files. (unless you remap the jsp servlet, but I wouldn't advise to do so).

The better approach is to define the variables in the .jsp including the .js, and pass these variables as arguments to an initializing function.

Also make sure you don't have isELIgnored="true"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...