将<string,string =“”>映射为Spring MVC中的@RequestParam </string,>

时间:2014-04-07 15:36:24

标签: spring-mvc

我是春天的新手,我正在尝试向我的控制器添加@RequestParam类型Map<String, String>,如下所示:

@RequestMapping(method = RequestMethod.GET)
public Model search(@RequestParam(value = "searchTerm", required = false) final String searchTerm,
        @RequestParam(value = "filters", required = false) Map<String, String> filters,
        final Model model) {

在网址中,它看起来像这样:

localhost/search?searchTerm=factory&filters[name]=factory1&filters[name]=factory2

但每次,过滤器都是null,无论我做什么。

可以这样做吗?非常感谢你的时间!

1 个答案:

答案 0 :(得分:1)

使用带有@RequestParam的Map来表示多个参数 如果方法参数是Map或MultiValueMap,则映射将填充所有查询字符串名称和值。以下内容将与/ employees / 234 / messages一起映射?sendBy = mgr&amp; date = 20160210

@RequestMapping("{id}/messages")
public String handleEmployeeMessagesRequest (@PathVariable("id") String employeeId,
                                        @RequestParam Map<String, String> queryMap,
                                        Model model) {
    model.addAttribute("msg", "employee request by id and query map : "+
              employeeId+", "+queryMap.toString());
    return "my-page";
}

其中employeeId =“234”且queryMap = {sendBy = mgr,date = 20160210}