如何让JavaScript使用服务器端的变量? [英] How to let JavaScript use the variable from server side?

查看:74
本文介绍了如何让JavaScript使用服务器端的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有jsp,它使用一些java bean来创建一些在jsp中使用的变量。我想知道如何用javascript共享这些变量?

We have jsp, which uses some java beans to create some variables to be used inside jsp. I am wondering how to share those variables with javascript?

推荐答案

将bean值写出为HTML元素的值或属性HTML元素。为HTML元素提供ID。使用Javascript按ID或其他访问者检索元素,您可以访问该值。你在使用struts还是其他Servlet框架?

Write out your bean value to an HTML element's value or an attribute of the HTML element. Give the HTML element an ID. Use Javascript to retrieve the element by ID, or by another accessor, and you can access the value. Are you using struts or another Servlet framework?

一个简单的例子

JSP:

 <div id="employeeName">
      <jsp:getProperty name="employee" property="firstName"/>
    </div>

HTML:

<script type="text/javascript">var el = document.getElementById("employeeName");</script>

这篇关于如何让JavaScript使用服务器端的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆