sonx
sonx

Reputation: 661

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, 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

Upvotes: 1

Views: 5972

Answers (2)

Noel M
Noel M

Reputation: 16116

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.

Upvotes: 5

Arjan
Arjan

Reputation: 21465

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

Upvotes: 0

Related Questions