【问题标题】:How to get params from a POST in Spring MVC?如何从 Spring MVC 中的 POST 获取参数?
【发布时间】:2013-06-18 12:24:58
【问题描述】:

在我的 js 文件中,我有一个 Ext.Ajax.request 从表中删除用户。最重要的是发送用户名。这是我的请求代码:

Ext.Ajax.request({
    url: 'deleteUser.html',
    method: 'POST',          
    headers: {'Content-Type': 'text/html'},
    waitTitle: 'Connecting',
    waitMsg: 'Sending data...',                                     
    params: {
        username:username
    },
    scope:this,
    success: received,                                    
    failure: function(){console.log('failure');}
});

在萤火虫中,我可以看到帖子 myapp/admin/deleteUser.html。在帖子选项卡中出现 username=example。

在服务器端,我有一个控制器来捕捉帖子:

 @RequestMapping(method=RequestMethod.POST, value="/deleteUser")
 public @ResponseBody Map<String,String> deleteUserHandler(@RequestParam("username")String username, Model model){
    userService.deleteUser(username);
    Map<String,String> responseMap = new HashMap<String, String>();
    responseMap.put("message", "Success");
    return responseMap;
 }

如果我有 @RequestParam("username") 我收到 400 错误(不正确的请求),如果我尝试其他方法来获取参数,我会得到空值。

有人可以帮帮我吗?

【问题讨论】:

    标签: spring-mvc extjs extjs4.2


    【解决方案1】:
    In the firebug i can see the post myapp/admin/deleteRole.html. 
    In the post tab appears username=example.
    

    你说你可以看到网络调用deleteRole.html

    但是在你的 ajax 和你的 Controller 中应该调用 "/deleteUser"

    如果您在控制器中记录某些内容,我们如何确定该函数是否已被调用。

    首先尝试正确映射到函数的 html 之间的连接

    @RequestMapping(method=RequestMethod.POST, value="/deleteUser")
    public @ResponseBody String deleteUserHandler(@RequestParam("username") String username){
        System.out.println("deleteUserHandler called");
        return "{\"message\" : \"success\"}";
    }
    

    如果你的函数调用它应该没问题..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 2011-07-20
      • 2016-06-06
      • 1970-01-01
      相关资源
      最近更新 更多