简体   繁体   English

调用jstl变量到javascript函数

[英]Call jstl variables to javascript function

File name : Test.jsp 档案名称:Test.jsp

I declared a variable under jstl tag 我在jstl标签下声明了一个变量

<%

String daysToActivate = Someclass.method();

%>

I want to call this method in the following javascript fumction 我想在以下javascript函数中调用此方法

//javascript function
function getDays(){



var x= daysToActivate;

alert(x);

}

// this alert message is not displaying.

please help 请帮忙

JavaScript doesn't know about Java variables declared in scriptlets and other places. JavaScript不了解在scriptlet和其他位置声明的Java变量。 It's because Java code is run and resolved on the server, and JavaScript is run in user's web browser. 这是因为Java代码在服务器上运行和解析,而JavaScript在用户的Web浏览器中运行。

To pass variable's value from Java to JavaScript put this script inside JSP page: 要将变量的值从Java传递到JavaScript,请将此脚本放入JSP页面:

function getDays(){
    var x = <%= daysToActivate %>;
    alert(x);
}

Value of daysToActivate will be resolved on the server side, inserted into HTML file as text, and send to the user's web browser. daysToActivate值将在服务器端解析,以文本形式插入HTML文件,然后发送到用户的Web浏览器。 Then on the client's side, from the web browser perspective this function will look like: 然后在客户端,从Web浏览器的角度来看,此功能将如下所示:

function getDays(){
    var x = 25;
    alert(x);
}

If daysToActivate is not an integer, then remember to use " : 如果daysToActivate不是整数,请记住使用"

function getDays(){
    var x = "<%= daysToActivate %>";
    alert(x);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM