简体   繁体   English

从javascript调用Java方法

[英]Call java method from javascript

I have a web jsp web application. 我有一个Web JSP Web应用程序。 In one page I need two dropdownlist. 在一页中,我需要两个下拉列表。 When I will select a value from dd1 then second dropdown will be fill according this value. 当我从dd1中选择一个值时,将根据该值填充第二个下拉列表。 How I can call java function from dropdown 1 change event in javascript or jQuery? 如何在JavaScript或jQuery中从下拉列表1更改事件调用Java函数?

I got example but that is calling jsp page but I need to java method and send parameter what i got from dropdown1 我有示例,但是正在调用jsp页面,但是我需要java方法并发送参数我从dropdown1获得的内容

This is my dropdown dd1. 这是我的下拉菜单dd1。 So if I select tom I have to related information of tom in dd2. 因此,如果选择tom,则必须在dd2中获取tom的相关信息。

            <td>
              <select  id="dd1">
                    <option value="1">john</option>
                    <option value="2">Tom</option>
              </select>

          </td>
        </tr>
        <tr>
          <th> Projects </th>
          <td>

              <select id="dd2">
                    <option value="1">pp1</option>
                    <option value="2">pp2</option>
              </select>

          </td>

I have following code 我有以下代码

$(function () {
    var ddVal = '';
    var dropdown = document.getElementById("dd1")
    $(dropdown).focus(function () {
        ddVal = $(this).val();
        }).blur(function () {
        if (ddVal == $(this).val()) {
            $(this).change();
        }
    }).change (function () {

});

For change event of dd1. 对于dd1的更改事件。 But I don't know how to call java method from jQuery. 但是我不知道如何从jQuery调用Java方法。

In my application I have a java class where there is a method which return list that I need to load in dd2 dropdown. 在我的应用程序中,我有一个Java类,其中有一个方法可以返回需要在dd2下拉列表中加载的列表。

Can anyone help me regarding this issue? 有人可以帮我解决这个问题吗?

You should do such things with AJAX. 您应该使用AJAX进行此类操作。

JavaScript sends the request, your controller utilizes the Java part to carry out what's needed and then passes back a JSON response, based on which your JavaScript manipulates the page. JavaScript发送请求,您的控制器利用Java部分执行所需的操作,然后传回JSON响应,JavaScript以此为基础对页面进行操作。


EDIT: 编辑:

For example, the script can issue the following request: 例如,脚本可以发出以下请求:

 $.ajax({ "type": "POST", "url": "/ajaxAPI/updatedropdown/", "data": { "selected": selectedMenuItemId }, "success": function (data) { var menuItems = data.menuItems; dropDownToChange.updateChoices(menuItems); } }); 

Your controller for such a request might look like: 您的此类请求的控制器可能如下所示:

 public class DropDownListController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MenuItem selected = extractSelectionFromRequest(request); List<MenuItem> choices = dropDownListService.getMenuFor(selected); ModelAndView mav = new ModelAndView("dropDownListAjax.jsp"); mav.addObject("menu", choices); } // WARNING! Some error checks are missing! private MenuItem extractSelectionFromRequeset(HttpServletRequest request) { validateRequest(request); return dropDownListService.getMenuItemById(request.getAttribute("selected")); } // ... } 

And for the view you could have something like: 对于视图,您可能会有类似的内容:

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> { "menuItems": [ <c:forEach items="${menu}" var="menuItem"> { "url": <c:out value="${menuItem['url']}"/>, "caption": <c:out value="${menuItem['caption']}"/> }, </c:forEach> ] } 

The client side JavaScript will then receive a response like: 然后,客户端JavaScript将收到如下响应:

 { "menuItems": [ { "url": "http://www.example.com/" "caption": "Home" }, { "url": "http://www.example.com/news/list/" "caption": "News" }, { "url": "http://www.example.com/forum/topics/" "caption": "Forum" }, ] } 

Please note that the above example might not be 100% correct as there have been a while since I last used JSP (and I'm more comfortable with FreeMarker anyways). 请注意,由于自上次使用JSP以来已经有一段时间了,因此上面的示例可能不是100%正确的(无论如何我对FreeMarker还是比较满意的)。

Basically, you invoke part of your web infrastructure that, rather than responding with HTML code, passes back JavaScript Objects based on the results of the requested operation. 基本上,您调用Web基础结构的一部分,而不是使用HTML代码进行响应,而是根据请求的操作的结果传回JavaScript对象。

Since jQuery is in the client side and JSP is on the server side, you don't have the option of directly calling the remote method. 由于jQuery在客户端,而JSP在服务器端,因此您无法直接调用远程方法。 The standard way to do this in the world of the Web is AJAX. 在Web领域中执行此操作的标准方法是AJAX。

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

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