【问题标题】:how to call a java class method in javascript using ajax ?如何使用 ajax 在 javascript 中调用 java 类方法?
【发布时间】:2012-03-30 19:11:28
【问题描述】:

我有一个 java 类 ::

package MyPackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.Gson;

public class PopulateTextbox {

    Gson gson = new Gson();
    JSONObject obj = new JSONObject();
    JSONArray arr = new JSONArray();
    String temp1;

    String temp;
    List <String>rowValues = new ArrayList<String>();
    List <Integer>rowValues1 = new ArrayList<Integer>();
    String[] contactListNames;
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;


    public String method(){


        try{


        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);

        String db = "jdbc:odbc:Practice_Database";
        con = DriverManager.getConnection(db,"","");

        st = con.createStatement();
        String sql = "SELECT Emp_Name,ID,Email_Add FROM EmployeeSearch";
        rs = st.executeQuery(sql);

        while(rs.next()){
            obj.put("ID", rs.getInt("ID"));
            obj.put("Names",rs.getString("Emp_Name"));
            obj.put("Email", rs.getString("Email_Add"));
            arr.add(obj);

        }
        //obj.accumulate("ID",rowValues1);

        //obj.accumulate("Names",rowValues);
        temp1 = arr.toString();
        System.out.println(temp1);

   }catch(Exception e){System.out.println(e);}
    /*finally{
        try {
                if(con!=null)con.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        try {
            if(rs!=null)rs.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }try {
            if(st!=null)st.close();

        } catch (SQLException e) {

            e.printStackTrace();
        }


    }*/
        return temp1;

    }
    public static void main(String args[])
    {
        PopulateTextbox obj = new PopulateTextbox();
        String temp1= obj.method();


    }
}

这将返回一个 Json 数组。现在我想在 JavaScript 中一次又一次地调用这个类的 method() 来获取新值,因为我更新了我的数据库。我有一个数据网格,它在页面第一次加载时工作正常,其中包含此 json 数组中的一组值。但是如何使用 Ajax 刷新数据。或者如何使用 Ajax 调用 method() 以便在我单击页面上的按钮时刷新数据。我在 java-script 中调用此方法的代码是 ::

<%

    String temp1;
    PopulateTextbox obj = new PopulateTextbox();
    temp1 = obj.method();
    %>

我在通过对服务器的 Ajax 调用检索 temp1 中的新值集时遇到问题。请帮忙 ?谢谢。

【问题讨论】:

  • 您提供的代码不是Javascript,它看起来像一个JSP scriptlet(完全是别的东西)。你到底在挣扎什么?您是否尝试过编写发出 AJAX 请求的 Javascript?

标签: java javascript ajax datagrid


【解决方案1】:

创建一个空白 JSP(例如,textBoxAjaxResp.jsp)并从该 JSP 中输出您的 JSON 字符串:

<%

String temp1;
PopulateTextbox obj = new PopulateTextbox();
temp1 = obj.method();
%>

<%=temp1 %>

并使用 jQuery AJAX 调用点击该 JSP。

$.get("mypath/textBoxAjaxResp.jsp", function (response) {
    //do operation using the response
}, "json");

【讨论】:

  • 你能详细解释一下吗?创建一个空白 JSP 意味着我不必在其中写任何内容。并且我的脚本的代码让将保留在哪个页面上?
  • 是一个空白的jsp并在该jsp中调用您的obj.method()并将json字符串从该jsp设置contentType输出到application/json
  • 嘿,我是你的代码。当我在浏览器上运行我的 textBoxAjaxResp.jsp 时,我将输出作为 json 数组。但是如何将它存储在 JavaScript 变量中,以便我可以将它传递给我的数据网格的源?
  • 修改了我的代码,这里的响应变量包含解​​析的json响应(js对象)。
  • 你能帮我解决这个问题吗? stackoverflow.com/questions/9702098/…
【解决方案2】:

您可以使用Direct Web Remoting 库通过javascript调用java函数

但是我建议自己写servlet(教程很老但是很好,如果可能的话,你应该使用servlet 3.0)(而不是使用DWR,它会让他们的servlet写入响应流,这也很好简单的需求,所以你应该自己编写servlet而不是使用第三方库)并直接编写JSON响应。

【讨论】:

    【解决方案3】:

    您可能想在这里查看我的答案Updating contents of a jsp page without refreshing 这使用 SpringMVC 类,该类使用 jquery ajax 从服务器端返回文本值到客户端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2016-09-10
      • 1970-01-01
      相关资源
      最近更新 更多