简体   繁体   English

将值从jsp发送到Java或将javascript发送到Java

[英]sending the value from jsp to java or javascript to java

I have a list of names in a table. 我在表中有一个名称列表。 When I click the name, it displays people under that particular name. 当我单击名称时,它会以该特定名称显示人员。 Ex: 例如:

aaa       bbb
          ccc 

Now I want to do moveover on aaa which l show 2 (number of ppl under aaa) in a tooltip. 现在,我想在aaa上进行移动,我会在工具提示中显示2(aaa下的ppl数)。

<a id="tool" title="toolTip" onmouseover="toolTip(localArrGroupNames[<%=in.intValue() %>])">

function toolTip(name){
  alert(name);
  //can I send this name to a java class ???

}

I get the name aaa in the localArrGroupNames[0].But I am unable to get the name in a java class where i count the number of people below the aaa. 我在localArrGroupNames [0]中获得了aaa的名称。但是我无法在java类中获得该名称,因为我无法计算aaa以下的人数。 How to send this value to java class? 如何将此值发送到java类? Please help me. 请帮我。

function toolTip(name){
   alert(name);
   //can I send this name to a java class ???

    new Ajax.Request('jspFile.jsp', {
        parameters: {
            name: name
        },
        method: 'get',
        onComplete: function(transport) {
            var resp = transport.responseText.strip();
            $('tool').update(resp);
        }
    });
}

A jsp is server side, the rendered HTML is client side so the javascript is running in a webbrowser on the client. jsp是服务器端,呈现的HTML是客户端,因此javascript在客户端的Web浏览器中运行。

To send any data from that client to your java class that is on the server you need to put in some form of request. 要将任何数据从该客户端发送到服务器上的java类,您需要以某种形式进行请求。

This can be simple by using a submit on your html form 通过在HTML表单上使用提交,这可以很简单

So you could have your tooltip javascript method store the chosen name in a hidden input field and then via submit pass it to the java class( usuually a servlet or some jsp ) 因此,您可以让您的工具提示javascript方法将所选名称存储在隐藏的输入字段中,然后通过提交将其传递给java类(通常是servlet或一些jsp)

Or more complex with the AJAX ( or similar ) frameworks that allow your javascript to pass the name to your server without having the entire page to be submitted 或更复杂的AJAX(或类似)框架允许您的javascript将名称传递到服务器,而无需提交整个页面

you need to submit a request to the server you can do this using ajax to perform asynchronously, or you could submit using a form in the jsp page. 您需要向服务器提交请求,可以使用ajax执行此请求以异步执行,也可以使用jsp页面中的表单提交请求。

You will of course need to supply the correct url (either form action, or ajax connection url), and have the appropriate servlet running on server. 您当然需要提供正确的url(表单操作或ajax连接url),并在服务器上运行适当的servlet

If doing ajax, most people use jquery and/or DWR 如果使用ajax,大多数人会使用jquery和/或DWR

Remember that Javascript runs on the client side only, while Java is server side only. 请记住,Javascript仅在客户端运行,而Java仅在服务器端运行。 Java can set a Javascript variable since Java is processed by the server first and then javascript, csss, and html is sent back to the browser for processing. Java可以设置Javascript变量,因为Java首先由服务器处理,然后将javascript,csss和html发送回浏览器进行处理。

To send Javascript variable to the server you must post your data from the client to the server. 要将Javascript变量发送到服务器,必须将数据从客户端发布到服务器。 One way to do that is to submit a form another is to create an Ajax request (XML over HTTP) there are lots of JS libraries out there that can help with doing Ajax. 一种方法是提交表单,另一种方法是创建一个Ajax请求(基于HTTP的XML),那里有很多JS库可以帮助完成Ajax。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM