Map multiple request parameters
I'm sending two parameters using GET (through URL) and I would like my request method to receive them like this...
Here's the controller:
@RequestMapping("/basketItems")
public String basketItems(
@RequestParam("fname") String firstName,
@RequestParam("lname") String lastName,
Model model) {
Customer customer = customerManager.getCustomer(firstName开发者_如何学Go, lastName);
Basket basket = basketManager.getBasket(customer.getReferenceNumber());
model.addAttribute("basket", basket);
model.addAttribute("totalItems", basketManager.getTotalNumberOfItems(basket));
model.addAttribute("totalPrice", basketManager.getTotalProductPrice(basket));
return "basketItems";
}
I get this error
org.springframework.web.bind.MissingServletRequestParameterException:Required java.lang.String parameter 'lname' is not present
Your HTTP request doesn't have the parameter lname
present. Either include that parameter in the request, or put required = "false"
on the annotation for lname
:
@RequestParam(value="lname", required="false")
If you put required = "false"
, then the variable assigned to lname
will be null
in that method, so be aware of that in your code.
For some more information, look at the relevant part of the Spring MVC documentation.
What is your request URI?
MissingServletRequestParameterException is thrown because there is no request parameter of type String with name lname to bind to variable lastName
精彩评论