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
Make sure that you're running a Servlet 2.4 capable container and that
web.xml
is declared as Servlet 2.4 or higher.This way EL will work in template text as well (without the need for JSTL
<c:out>
). It was namely introduced in Servlet 2.4 / JSP 2.0. Tomcat 5.5 is an example of a Servlet 2.4 container. If your container supports a higher servlet API version, e.g. 2.5 or 3.0, then you should declare theweb.xml
conform this version to benefit all newest features.You'll then also be able to do the following in the JSP:
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"
An example to Bozho suggestion, this way i worked facebook login.
Scenario:
I have a property in application.properties and i need this value in
facebook-login-sdk.js
initialize facebook login. So i read it in javaLoginController
via below code, then read it inlogin.jsp
and assign a global variableapplicationFacebookId
then use this global variable infacebook-login-sdk.js
file.IMPORTANT NOTE : Read variable name applicationFacebookId before include facebook-login-sdk.js. Because you will use a global variable so you want to set it before facebook-login-sdk.js called.
application.properties
LoginController.java
login.jsp
facebook-login-sdk.js
I think you can using function eval() ,just like as follow:
if you want pass a JavaBean to a js var ,you should override toString() method.