简体   繁体   English

是否有用于从javascript到Java bean(spring bean)执行RPC的框架?

[英]Is there any framework for performing RPC from javascript to Java bean(spring bean)?

This is the short description of my situation... 这是我的情况的简短描述...

  • I have spring bean BookingServiceImpl which implements BookingService 我有一个实现BookingService的spring bean BookingServiceImpl
  • BookingService has method: RoomDescription getRoomDescriptionById(String roomId); BookingService具有方法:RoomDescription getRoomDescriptionById(String roomId);
  • I also have JSP page with javascript on it 我也有带有javascript的JSP页面
  • I want to do something like this in javascript 我想在javascript中做这样的事情

    var roomDescription = bookingService.getRoomDescriptionById(222); var roomDescription = bookingService.getRoomDescriptionById(222);

    /// ... use roomDescription /// ...使用roomDescription

The question is: is there any existent framework which does things like that (magically allows to cal spring beans from javascript without additional boiler-plate code, assuming all parameters and method results are JSON-friendly)? 问题是:是否存在类似这样的框架(假设所有参数和方法结果都是JSON友好的,那么它就可以从javascript中校准Spring bean,而无需其他示例代码)?

Spring provides mechanisms to expose RESTful endpoints via the Web MVC framework Spring提供了通过Web MVC框架公开RESTful端点的机制

The documentation is very good. 该文档非常好。 A Controller definition for your specific example could look like this: 您的特定示例的Controller定义如下所示:

@Controller
@RequestMapping(value = "/rooms/{roomId}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Room getRoom(@PathVariable String roomId, ModelMap modelMap) {    
    return bookingService.getRoomById(roomId);
}

On the javascript side, you could use jQuery to make an ajax call to retrieve the data. 在javascript方面,您可以使用jQuery进行ajax调用以检索数据。 Here is an example of what that could look like: 这是看起来可能的示例:

$.getJSON('http://yourserver.com/rooms/222',
    function(room) {
       // Do something with room.description
    });

This is a basic example (without proper error handling, security, etc). 这是一个基本示例(没有适当的错误处理,安全性等)。 It's the closest thing to an existent framework that I'm aware of for Spring RESTful calls from javascript. 这是我所知道的从javascript进行Spring RESTful调用时最接近的现有框架。 If you need to access the Room data on the client side javascript then you'll need to expose it via some Spring construct (eg a Controller) 如果您需要在客户端javascript上访问Room数据,则需要通过某些Spring构造(例如Controller)公开它

Take a look at DWR. 看看DWR。 This is as close as you will get to creating js clients. 这与创建js客户端差不多。 http://directwebremoting.org/dwr/documentation/server/integration/spring.html http://directwebremoting.org/dwr/documentation/server/integration/spring.html

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

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