【发布时间】:2013-09-30 04:56:18
【问题描述】:
是否可以从同一页面上的javascript访问jsp中定义的String类型变量?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>
<script type="text/javascript">
foo();
function foo()
{
var value = "<%=myVar%>";
alert(value);
}
</script>
</head>
<body>
<%
String myVar="blabla";
%>
</body>
</html>
在 Eclipse 中出现错误
myVar cannot be resolved to a variable
【问题讨论】:
-
你的 myVar 是在哪里定义的?
-
<%和<%=块按照它们在页面上的顺序进行评估。您需要将声明myVar的那个放在使用它的那个之前。 -
尽管理想情况下,您根本不会使用 scriptlet。
-
为什么不使用 scriptlet?