如何使用Spring MVC RequestMapping获得多个参数?

时间:2014-02-21 01:53:12

标签: java spring url spring-mvc

我在这里使用Spring MVC RequestMapping作为GET参数。以下是我的代码 -

@RequestMapping(value = "index", method = RequestMethod.GET)
public HashMap<String, String> handleRequest(@RequestParam("dc1Servers") String dc1Servers) {
    HashMap<String, String> model = new HashMap<String, String>();
    String helloWorld = "Hello World!";
    model.put("greeting", helloWorld);

    System.out.println(dc1Servers);

    return model;
}

我正在点击此网址 - http://127.0.0.1:8080/dataweb/index?dc1Servers=7然后它进入上面的代码并在控制台上打印出7并且正常工作。

现在我想要以下两个参数 -

dc2Servers=7
dc3Servers=7

所以我做了一个这样的方法,它可以带三个输入参数 -

@RequestMapping(value = "index", method = RequestMethod.GET)
public HashMap<String, String> handleRequest(@RequestParam("dc1Servers") String dc1Servers, @RequestParam("dc2Servers") String dc2Servers, @RequestParam("dc3Servers") String dc3Servers) {
    HashMap<String, String> model = new HashMap<String, String>();
    String helloWorld = "Hello World!";
    model.put("greeting", helloWorld);

    System.out.println(dc1Servers);
    System.out.println(dc2Servers);
    System.out.println(dc3Servers);

    return model;
}

现在,如果我按这样打到网址,那么它就不起作用了 -

http://127.0.0.1:8080/dataweb/index?dc1Servers=7?dc2Servers=7?dc3Servers=7

它给了我一些错误..知道我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

这应该是

http://127.0.0.1:8080/dataweb/index?dc1Servers=7&dc2Servers=7&dc3Servers=7

再试一次

  

&安培;适用于每个参数

     

?仅适用于url参数的开头

检查此示例

  

http://www.example.com/products/women/dresses?sessionid=34567&source=google.com

相关问题