Rhino variables in JSP
I have this in my jsp:
<%
Scriptable scope = Context.enter().initStandardObjects();
scope.put( "foo", scope, Context.toObject( "foo", scope ) );
%>
<script type="text/javascript">
function doSomething() {
// this works fine, so I can tell Rhino is working to some extent
var now = new Packages.java.util.Date();
alert(now);
// but this comes back as an undefined variable - why?
alert(foo);
}
</script>
I'm completely baffled - I thought that by putting the String value ("foo") in the default scope I'd be able to see it in the javascript context, yet, I cannot. This would make sense to me if it weren't for the开发者_运维问答 fact that the 'now' variable works fine.
Does anyone know what I'm doing wrong here?
Nope, can't access it directly. You have to use JSP tagging to get to it. Something like <%= foo %>.
There's a tutorial here.
And now I re-read and see you're doing this in a function. Using the above should still work. However, it'd probably be a better practice to pass the value into the function and keep the JSP out of your function declaration.
精彩评论