如何将 EL 变量传递给 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5155885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:52:10  来源:igfitidea点击:

How to pass an EL variable to javascript

javascriptjspel

提问by Dave819

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.

我有一个变量,${bean.name}如何将它传递给 javascript var?我试过了 var name = "${bean.name}"var name = ${bean.name}但它不起作用。

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

我的想法是把它放在一个隐藏的输入中,就像一个隐藏的 <input id="test" type="text" value"${bean.name}">

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

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

这不起作用要么var name成为字符串"${bean.name}"

note. i can't use jstl

笔记。我不能使用 jstl

回答by Bozho

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

您不能在.js文件中评估 JSTL 。仅在.jsp文件中。(除非您重新映射 jsp servlet,但我不建议这样做)。

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

更好的方法是在 .jsp 中定义变量,包括 .js,并将这些变量作为参数传递给初始化函数。

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

还要确保你没有 isELIgnored="true"

回答by BalusC

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

这不起作用要么 var name 变成字符串“${bean.name}”

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

确保您正在运行支持 Servlet 2.4 的容器,web.xml并且声明为 Servlet 2.4 或更高版本。

<?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.xmlconform this version to benefit all newest features.

这样 EL 也可以在模板文本中工作(不需要 JSTL <c:out>)。它是在 Servlet 2.4 / JSP 2.0 中引入的。Tomcat 5.5 是 Servlet 2.4 容器的一个示例。如果您的容器支持更高的 servlet API 版本,例如 2.5 或 3.0,那么您应该声明web.xml符合此版本以使所有最新功能受益。

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

然后,您还可以在 JSP 中执行以下操作:

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

回答by zhiwen zhang

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

我认为您可以使用函数eval(),如下所示:

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

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

如果要将 JavaBean 传递给 js var,则应覆盖 toString() 方法。

回答by erhun

An example to Bozho suggestion, this way i worked facebook login.

Bozho 建议的一个例子,我通过这种方式登录了 facebook。

Scenario:

设想:

I have a property in application.properties and i need this value in facebook-login-sdk.jsinitialize facebook login. So i read it in java LoginControllervia below code, then read it in login.jspand assign a global variable applicationFacebookIdthen use this global variable in facebook-login-sdk.jsfile.

我在 application.properties 中有一个属性,我在facebook-login-sdk.js初始化 facebook 登录时需要这个值。所以我LoginController通过下面的代码在java中读取它,然后读取它login.jsp并分配一个全局变量applicationFacebookId然后在facebook-login-sdk.js文件中使用这个全局变量。

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.

重要说明:在包含 facebook-login-sdk.js 之前阅读变量名称 applicationFacebookId。因为您将使用一个全局变量,所以您想在调用 facebook-login-sdk.js 之前设置它。

application.properties

应用程序属性

security.application.facebookId=yourApplicationFacebookid

LoginController.java

登录控制器.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

登录.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

facebook-登录-sdk.js

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