Spring @RequestParam参数未在POST方法中传递

我对Spring和发布请求有疑问。我正在为Ajax调用设置控制器方法,请参见下面的方法定义

@RequestMapping(value = "add.page", method = RequestMethod.POST)

@ResponseBody

public Object createComment(

@RequestParam(value = "uuid", required = false) String entityUuid,

@RequestParam(value = "type", required = false) String entityType,

@RequestParam(value = "text", required = false) String text,

HttpServletResponse response) {

....

无论我以哪种方式进行HTML调用,@RequestParam参数的值始终为null。我还有许多其他看起来像这样的方法,主要的区别是其他方法是GET方法,而这个方法是POST。不能@RequestParam与POST方法一起使用吗?

我正在使用Spring 3.0.7.RELEASE版本-有人知道问题的可能原因吗?


Ajax代码:

$.ajax({

type:'POST',

url:"/comments/add.page",

data:{

uuid:"${param.uuid}",

type:"${param.type}",

text:text

},

success:function (data) {

//

}

});

回答:

问题原来是我调用该方法的方式。我的ajax代码正在传递请求正文中的所有参数,而不是将其作为请求参数传递,这就是为什么我的@RequestParam参数都为空的原因。我将ajax代码更改为:

$.ajax({

type: 'POST',

url: "/comments/add.page?uuid=${param.uuid}&type=${param.type}",

data: text,

success: function (data) {

//

}

});

我还更改了控制器方法以从请求正文中获取文本:

@RequestMapping(value = "add.page", method = RequestMethod.POST)

@ResponseBody

public Object createComment(

@RequestParam(value = "uuid", required = false) String entityUuid,

@RequestParam(value = "type", required = false) String entityType,

@RequestBody String text,

HttpServletResponse response) {

现在,我得到了期望的参数。

以上是 Spring @RequestParam参数未在POST方法中传递 的全部内容, 来源链接: utcz.com/qa/432091.html

回到顶部