如何在 Spring-MVC 上处理具有多个参数的请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29191043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle a request with multiple parameters on Spring-MVC
提问by Hyman
I am sending following request that need to be handled by Spring-MVC but it does not.
我正在发送需要由 Spring-MVC 处理的以下请求,但它没有。
http://localhost:2001/MyProject/flights/find?origin=LAX&destination=WA&departure=&arrival=&return=false
Code
代码
@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) {
回答by manish
That is not the correct way (or place) to use @PathVariable
. You need to use @RequestParam
.
这不是使用@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) { ... }
}
Note that return
is a keyword in Java so you cannot use it as a method parameter name.
请注意,这return
是 Java 中的关键字,因此您不能将其用作方法参数名称。
You will also have to add a java.beans.PropertyEditor
for reading the dates because the dates will (presumably) be in a specific format.
您还必须添加一个java.beans.PropertyEditor
用于读取日期,因为日期将(大概)采用特定格式。
回答by Mai Tan
Try this, may be it works:
试试这个,可能有效:
@RequestMapping("/find")
public String findFlight(@RequestParam("origin") String origin
, @RequestParam("destination") String destination,....