How to pass an EL variable to javascript

2019-01-08 01:45发布

问题:

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

回答1:

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"



回答2:

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

Make sure that you're running a Servlet 2.4 capable container and that web.xml is declared as Servlet 2.4 or higher.

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <!-- Config here. -->

</web-app>

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 the web.xml conform this version to benefit all newest features.

You'll then also be able to do the following in the JSP:

<script type="text/javascript">var name = '${bean.name}';</script>


回答3:

I think you can using function eval() ,just like as follow:

var name = eval("${bean.name}");

if you want pass a JavaBean to a js var ,you should override toString() method.



回答4:

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 java LoginController via below code, then read it in login.jsp and assign a global variable applicationFacebookId then use this global variable in facebook-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

security.application.facebookId=yourApplicationFacebookid

LoginController.java

@Value("${security.application.facebookId}")
private String applicationFacebookId;

@RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpServletRequest request, Model model) {
        model.addAttribute("applicationFacebookId", applicationFacebookId);
        return "login";
    }

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script>
    var applicationFacebookId = '${applicationFacebookId}';
    //alert('applicationFacebookId:' + applicationFacebookId);
</script>
<title>Login Page</title>
<script src="<c:url value="/static/js/facebook-login-sdk.js" />"></script>
</head>
</html>

facebook-login-sdk.js

window.fbAsyncInit = function() {
    FB.init({
        appId : applicationFacebookId,
        cookie : true, // enable cookies to allow the server to access
        xfbml : true,
        version : 'v2.7'
    });