如何在Spring-MVC上处理具有多个参数的请求

时间:2015-03-22 04:36:17

标签: spring spring-mvc

我发送的请求需要由Spring-MVC处理,但事实并非如此。

http://localhost:2001/MyProject/flights/find?origin=LAX&destination=WA&departure=&arrival=&return=false

代码

@Controller
@RequestMapping("/flights")
public class FlightController {

   @RequestMapping(value = "/find?origin={origin}&destination={destination}&departure={departure}&arrival={arrival}&return={return}", method = RequestMethod.GET)
    public String findFlight(@PathVariable String origin,
            String destination, Date departure, Date arrival, boolean return) {

2 个答案:

答案 0 :(得分:14)

这不是使用@PathVariable的正确方法(或地点)。您需要使用@RequestParam

@Controller
@RequestMapping("/flights")
public class FlightController {
  @RequestMapping("/find")
  public String findFlight(@RequestParam String origin
                          , @RequestParam String destination
                          , @RequestParam(required = false) Date departure
                          , @RequestParam(required = false) Date arrival
                          , @RequestParam(defaultValue = "false", required = false, value = "return") Boolean ret) { ... }
}

请注意,return是Java中的关键字,因此您不能将其用作方法参数名称。

您还必须添加java.beans.PropertyEditor来阅读日期,因为日期(大概)将采用特定格式。

答案 1 :(得分:0)

试试这个,可能会有效:

 @RequestMapping("/find")
  public String findFlight(@RequestParam("origin") String origin
                          , @RequestParam("destination") String destination,....