简体   繁体   English

在JSP中从JavaScript / jQuery调用后端Java方法

[英]Calling a backend Java method from JavaScript/jQuery in JSP

I have a JSP in which there is a select list containing entity kind names. 我有一个JSP,其中有一个包含实体类名称的select列表。 When I select an entity kind I need to populate another select list with the field names of the selected entity kind. 当我选择实体类型时,我需要使用所选实体类型的字段名称填充另一个select列表。 For that I call a JavaScript function on the onchange event. 为此,我在onchange事件上调用了一个JavaScript函数。

In the JavaScript method I need to call a method in the backend that returns an arraylist that contains the field names of the selected entity kind. 在JavaScript方法中,我需要在后端调用一个方法,该方法返回一个包含所选实体类型的字段名称的arraylist

How do I call the method with and without Ajax? 如何使用和不使用Ajax调用该方法? Also how do I populate the second select list dynamically with the arrayList ? 另外如何使用arrayList动态填充第二个选择列表?

I'll describe two ways to go: with/without AJAX. 我将描述两种方法:使用/不使用AJAX。

  1. If you want to do a synchronous form submit you'll need to attach onchange event to your first select element: 如果要进行同步表单提交 ,则需要将onchange事件附加到第一个select元素:

     <select name="select-one" id="select-one" onchange="this.form.submit()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> 

    When done this way, the form will be submitted and first select option will be available as request.getParameter("select-one") , based on which you'll provide data for second dropdown population, typically forwarding to a JSP. 以这种方式完成后,表单将被提交,并且第一个选择选项将作为request.getParameter("select-one")提供,您将根据该选项为第二个下拉填充提供数据,通常转发到JSP。

  2. If you want to retrieve a list via AJAX and repopulate another dropdown, you can send an AJAX request and handle returned data in a callback function: 如果要通过AJAX检索列表并重新填充另一个下拉列表,则可以发送AJAX请求并在回调函数中处理返回的数据:

     var val = $('#select-one option:selected').val(); $.ajax({ url: "servletURL",//servlet URL that gets first option as parameter and returns JSON of to-be-populated options type: "POST",//request type, can be GET cache: false,//do not cache returned data data: {one : val},//data to be sent to the server dataType: "json"//type of data returned }).done(function(data) { var second = $("#select-two"); $.each(data, function() { options.append($("<option />").val(this.value).text(this.label)); }); }); 

    When done this way, the second dropdown will be repopulated without page refresh. 以这种方式完成后,将重新填充第二个下拉列表而不刷新页面。

  1. Write a JavaScript function names callAJAX on the onchage event of your select drop down 在选择下拉列表的onchage事件上编写一个JavaScript函数名称callAJAX

  2. In your callAJAX function , make an ajax call to the back end, get the response from the server, and populate the new drop down with the response coming in your ajax call 在你的callAJAX函数中,对后端进行ajax调用,从服务器获取响应,并使用ajax调用中的响应填充新的下拉列表

I hope you can make ajax calls , if not let me know. 如果不让我知道,我希望你可以拨打ajax电话。

You want to load your list dynamically from backend . 您想要从后端动态加载列表。 You must communicate with your server either: 您必须与服务器通信:

  • with a page load (form submit) 页面加载(表单提交)
  • or without a page load(ajax). 或没有页面加载(ajax)。

If AJAX is not your requirement, I suggest you do it by form submit(with page load) first because it's simple and easier for beginner. 如果AJAX不是您的要求,我建议您首先通过表单提交(带页面加载)来实现,因为它对初学者来说简单易用。

Agree with Jai. 同意Jai。 You will have to submit that form to the java method, then your java method will return the arrayList. 您必须将该表单提交给java方法,然后您的java方法将返回arrayList。 Of course if you form submit, your page will be refreshed and I'm not sure if your previously selected values will still be selected on the form. 当然,如果您进行提交,您的页面将会刷新,我不确定您之前选择的值是否仍会在表单上选中。 I'm not too clued up with this method of doing it. 我不太习惯这种做法。 I prefer to use jquery. 我更喜欢使用jquery。

With jquery you can do it like this: 使用jquery,你可以这样做:

$.ajax({
    url: "/MyApp/MyClass/getArrayList",
    type: "GET",
    data: "selectedEntity=" + s_entity,
    success: function(response){ 
        //handle returned arrayList
    },
    error: function(e){  
        //handle error
    } 
});

Put this in a function. 把它放在一个函数中。 Pass your selected entity as a parameter and handle the response in the success part. 将您选择的实体作为参数传递,并在成功部分中处理响应。 Of course your java method should map 'selectedEntity' to a parameter in the method header. 当然,您的java方法应该将'selectedEntity'映射到方法头中的参数。 In Spring it's done like this: 在Spring中,它是这样完成的:

private @ResponseBody ArrayList getArrayList(@RequestParam("selectedEntity") String entity)

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

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