不使用servlet从html调用Java方法 [英] Calling Java Method from html without using a servlet

查看:905
本文介绍了不使用servlet从html调用Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 index.html 有一定的视图,我想通过调用java方法更新此视图(不重定向或重新加载页面),使用Web服务并检索数据。我想在 index.html 中显示数据。我尝试使用servlet,但它重定向到另一个url,这不是我想要做的。

I have my index.html that has a certain view, and I want to update this view (without redirecting or reloading the page) by calling a java method, that consumes a web service and retrieves data. I want to display the data in my index.html. I tried using a servlet but it redirects to another url and that is not what I want to do.

有没有办法在我的 index.html 中调用java或显示java类的属性?

Is there a way to call a java or display attributes of a java class within my index.html?

推荐答案

一种方法是将您的Java功能公开为REST服务。

One approach is to expose your Java functionality as a REST service.

泽西岛REST终点

@Path("/rest/emp/")
public class EmployeeService {
    @GET
        @Path("/{param}")
        public EmployeDTO getMsg(@PathParam("param") String id) {



            return getEmployeeDetails(id);

        }

    }

EmployeDTO.java

String name;
String id;
String address;
//getters and setters

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Sample Page</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script src="employee.js"></script>
    </head>

    <body>
        <div>
            <p id="eid">The ID is: </p>
            <p id="eName">The Employee Name: </p>
            <p id="eAddress">The Employee Address:</p>
        </div>
    </body>
</html>

employee.js

$(document).ready(function() {
    $.ajax({
        url: "/rest/emp/10" (Your Rest end point URL)
    }).then(function(data) {
       $('#eid').append(data.id);
       $('#eName').append(data.name);
       $('#eAddress').append(data.address);

    });
});

这篇关于不使用servlet从html调用Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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