将JavaScript中的值分配给Java变量 [英] Assigning values from JavaScript to Java variable

查看:76
本文介绍了将JavaScript中的值分配给Java变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JSP页面中将JavaScript中的值分配给Java变量?可以这样做吗?

How to assign a value from JavaScript to Java variable in the JSP page? Is it possible to do this?

以下是javascript函数:

The following is the javascript function:

function loadGroupMembers(beanArrayVal){
    document.getElementById("beanVal").value=beanArrayVal;

   ------------I have mentioned my requirement below --------------

   %%%%%%%%%%%%%%%

   int beanArrVal =beanArrayVal ;

   %%%%%%%%%%%%%%

   ( or )

   %%%%%%%%%%

   c:set  var="beanvalue" value="${beanArrayVal}"

   %%%%%%%%%%%%%

}

即使在上述相同功能中,我也使用id beanVal在隐藏的输入字段中分配了beanArrayVal值.即使可以将隐藏字段中的值分配给JSTL变量也足够了.

Even I have assigned that beanArrayVal value in hidden input field too using id beanVal in the same above mentioned function. It's good enough even if is possible to assign value from this hidden field to JSTL variable.

推荐答案

JS在客户端计算机上运行. Java在服务器计算机上运行.它们之间唯一的通信工具是HTTP. Java/JSP可以生成/生成HTML页面,并将其作为HTTP响应从服务器发送到客户端,因此,只需在JSP模板中按原样生成Java变量,就可以轻松地(预先)设置Java变量.但是反过来,实际上需要从客户端到服务器的HTTP请求.您可以通过单击链接或提交表单来手动或在Javascript很少帮助下(例如link.click()form.submit())同步调用HTTP请求.您也可以借助 Ajaxical 功能异步调用HTTP请求.

JS runs at the client machine. Java runs at the server machine. The only communication tool between those is HTTP. Java/JSP can generate/produce a HTML page and send it from server to client as a HTTP response, thus it can easily (pre)set Javascript variables by simply generating them as-is in the JSP template. But the other way round really requires a HTTP request from the client to the server. You can invoke HTTP requests synchronously by clicking a link or submitting a form, either manually or with little help of Javascript like link.click() or form.submit(). You can also invoke HTTP requests asynchronously with help of Ajaxical powers.

长话短说:让JS将其设置为(隐藏的)输入值/请求参数,然后通过提交带有(隐藏的)输入值的表单或调用带有请求参数的链接,将其发送到服务器端,或通过使用查询字符串触发ajax请求.

Long story short: let JS set it as a (hidden) input value / request parameter and send it to the server side by submitting the form with the (hidden) input value, or by invoking a link with the request parameter, or by firing an ajax request with a query string.

要了解有关Java/JSP和Javascript之间的隔离墙的更多信息,您可能还会发现这篇文章也很有用:

To learn more about the wall between Java/JSP and Javascript, you may find this article useful as well: Java/JSP/JSF and JavaScript.

希望这可以清理很多东西,并为您打开一个新世界.

Hope this clears a lot of things up and opens a new world for you.

这篇关于将JavaScript中的值分配给Java变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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