使用jquery调用java服务器端方法

问题描述:

我需要在jquery中调用java类方法,我该怎么做?

I need to call java class method inside jquery , how do i do that ?

更多理解Ex:

Class Foo{
      public static void letsCall(){
          // Do something here ...
      }
}

我想称之为 letsCall()方法来自jquery。

I want to call this letsCall() Method from jquery.


1方法: -

1 approach:-

在jquery api中,要调用任何后端系统,在你的情况下java,你需要做ajax调用来调用你的java控制器或服务。

in jquery api, to call any backend system , in your case java, you need to do ajax call to invoke your java controller or service.

jquery ajax post call(调用你的java控制器)这里)


第二种方法: -

2nd approach:-

如果你需要java类参与你的jsp编队,你可以去自定义标签,在你的情况下你可以找到jsf框架,它基本上将你的用户事件绑定到你的控制器。

if you need java class to take part in your jsp formation , you can go for custom tags, in your case you can look for jsf framework which basically bind your user events to your controller.

其他框架 - springmvc将你的jsp属性绑定到相应的模型,在这种情况下不需要使用jquery,你可以通过spring自定义标签实现它。

other frameworks - springmvc bind your jsp attributes to its corresponding models, in this case no need to use jquery , you can achieve it through spring custom tags.

spring mvc example

春季自定义代码示例

spring自定义标签示例