Jquery: post data to a jsp method?

自古美人都是妖i 提交于 2019-12-25 04:23:08

问题


I am currently a asp.net guy, but have to do some jsp work now. I know that in asp.net, you can define a public static method with [WebMethod] attribute e.g.

  [WebMethod]
  public static string GetIt(string code)
  {
    return GetSomething(code);
  }

then, I can call this method in jquery

$.ajax({
  type: "POST",
  url: "PageName.aspx/GetIt",
  data: "{'code':'+$("#code").val()+"'}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

can someone give me some light how to do it using jsp?


回答1:


With pure jsp and servlets you'll have to do a lot of things to achieve that.

It can easily be done with spring-mvc, almost in the same way you showed:

  • create a class and annotate it with @Controller
  • create the method to be like this:

    @RequestMapping("/path/get")
    @ResponseBody
    public String getIt() {
        ...
    }
    
  • you'll need <mvc:annotation-driven /> and jackson on your classpath.

Note that you are calling "get", so the logical http method is GET, rather than POST



来源:https://stackoverflow.com/questions/3985464/jquery-post-data-to-a-jsp-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!