Calling Java Method from html without using a servlet

后端 未结 3 709
借酒劲吻你
借酒劲吻你 2021-01-27 12:59

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 w

3条回答
  •  野性不改
    2021-01-27 13:46

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

    Jersey REST end point

    @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

    
    
        
            Sample Page
            
            
        
    
        
            

    The ID is:

    The Employee Name:

    The Employee Address:

    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);
    
        });
    });
    

提交回复
热议问题