在脚本标签内使用(从servlet导入的)对象 [英] Use an imported object (from the servlet) inside a script tag
问题描述
我的servlet向JSP页面发送一个对象.该对象具有一些属性,根据这些属性,我想在悬停操作期间更改颜色.一种解决方案是直接在我的JSP页面中更改悬停样式.但是我想知道,我可以以某种方式在script标签内使用该对象并从中做出决定吗?我发现此,但它没有用(或者我用错了方式)!
My servlet sends to the JSP page an object. This object has some attributes and depending on these attributes i want to change the color during a hover action. One solution is to change the hover style directly in my JSP page. But i was wondering, can i use somehow this object inside the script tag and take the decisions from there? I found this but it didn't work (or i use it a wrong way)!
<script>
$(document).ready(function() {
//import myObject;
});
</script>
推荐答案
您的JSP被呈现在服务器上并发送到客户端. 客户端(浏览器)对您的Java/JSP代码一无所知.
Your JSP gets rendered on the server and sent to the client. The client (browser) does not know anything about your Java/JSP code.
但是,您可以将Java对象的属性呈现到呈现的页面中,例如:
But, you can render properties of your Java object into the rendered page, like:
<script>
var stringVariable = "${myObject.myStringProperty}";
var intVariable = ${myObject.myIntProperty};
</script>
这将呈现在您的服务器上,浏览器将显示为:
This will be rendered on your server, and the browser will see it like:
<script>
var stringVariable = "Hello World!";
var intVariable = 4711;
</script>
使用此技术,您可以在需要的任何位置(HTML,CSS,Javascript)在客户端上使用服务器端变量.
Using this technique, you can use your server side variables on the client, wherever you need them (HTML, CSS, Javascript).
这篇关于在脚本标签内使用(从servlet导入的)对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!