jQuery:将数据发布到jsp方法? [英] Jquery: post data to a jsp method?
问题描述
我目前是一个asp.net的人,但是现在必须做一些jsp的工作.我知道在asp.net中,您可以使用[WebMethod]
属性定义一个公共静态方法.
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);
}
然后,我可以在jquery中调用此方法
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.
}
});
有人可以给我一些使用jsp的方法吗?
can someone give me some light how to do it using jsp?
推荐答案
使用纯jsp和servlet,您将需要做很多事情来实现这一目标.
With pure jsp and servlets you'll have to do a lot of things to achieve that.
使用 spring-mvc ,几乎与您显示的方式相同:
It can easily be done with spring-mvc, almost in the same way you showed:
- 创建一个类并用
@Controller
对其进行注释
-
创建如下所示的方法:
- create a class and annotate it with
@Controller
create the method to be like this:
@RequestMapping("/path/get")
@ResponseBody
public String getIt() {
...
}
在类路径上需要<mvc:annotation-driven />
和jackson.
you'll need <mvc:annotation-driven />
and jackson on your classpath.
请注意,您正在调用"get",因此逻辑http方法是GET,而不是POST
Note that you are calling "get", so the logical http method is GET, rather than POST
这篇关于jQuery:将数据发布到jsp方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!