最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jsp - How to pass an EL variable to javascript - Stack Overflow

programmeradmin1浏览0评论

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 bees the string "${bean.name}"

note. i can't use jstl

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 bees the string "${bean.name}"

note. i can't use jstl

Share Improve this question edited Mar 1, 2011 at 17:31 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Mar 1, 2011 at 14:32 Dave819Dave819 6013 gold badges11 silver badges14 bronze badges 6
  • are you trying to pass a JSON into JavaScript or something? – KJYe.Name Commented Mar 1, 2011 at 14:35
  • it works for me. ;) you must be more specific about how it doesn't work. – Bozho Commented Mar 1, 2011 at 14:37
  • @KJY112 No i dont think its JSON – Dave819 Commented Mar 1, 2011 at 14:42
  • @Bozho it doesn't work as i my name variable bees the string "${bean.name}" and not the actual value of bean.name – Dave819 Commented Mar 1, 2011 at 14:43
  • 1 @Dave819 - are you putting the code into a static file (maybe a .js file?) that isn't being parsed by the JSP interpreter? – Spudley Commented Mar 1, 2011 at 14:46
 |  Show 1 more ment

4 Answers 4

Reset to default 7

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"

this doen't work either var name bees 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./xml/ns/j2ee"
    xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun./xml/ns/j2ee http://java.sun./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>

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.

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'
    });
发布评论

评论列表(0)

  1. 暂无评论