Spring MVC Tutorial
Core Spring MVC
Spring MVC - Annotation
Spring MVC - Form Handling
Spring MVC with JSTL
Spring MVC with REST API
Spring MVC with Database
In Spring MVC, both query strings and query parameters refer to the part of a URL that comes after a ?
symbol. They represent additional data sent to the server during an HTTP request. However, there are subtle differences in their terminology:
Query String:
http://example.com/find?name=John&age=25
, the entire name=John&age=25
portion is the query string.Query Parameter:
name=John
and age=25
are two distinct query parameters.In Spring MVC, you can capture these query parameters using the @RequestParam
annotation.
Consider the scenario where a user wants to search for a product with specific criteria. The request URL might look like:
http://example.com/products?name=phone&brand=apple
Here's how you can capture these parameters in a Spring MVC controller:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class ProductController { @GetMapping("/products") public ModelAndView getProducts( @RequestParam(name = "name", required = false) String name, @RequestParam(name = "brand", required = false) String brand) { ModelAndView modelAndView = new ModelAndView("productList"); // Assuming you have a service to fetch products based on name and brand. // List<Product> products = productService.findByNameAndBrand(name, brand); // modelAndView.addObject("products", products); // For demonstration: modelAndView.addObject("message", "Search criteria: name=" + name + ", brand=" + brand); return modelAndView; } }
In the above code:
@RequestParam
annotation binds the value of the query parameter with the given name to the method parameter.required = false
means that the query parameter is optional, and the request will not fail if it's missing.With this setup, when you navigate to the given URL, the controller will capture the query parameters name
and brand
from the query string and process them accordingly.
Spring MVC query string example:
Description: In Spring MVC, you can handle query parameters by using the @RequestParam
annotation in the controller method.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processQueryString(@RequestParam String param) { // Logic to process the query parameter return ResponseEntity.ok("Received parameter: " + param); } }
RequestMapping query parameters Spring MVC:
Description: You can use @RequestMapping
with query parameters to handle requests with specific parameters.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processQueryString(@RequestParam String param) { // Logic to process the query parameter return ResponseEntity.ok("Received parameter: " + param); } }
@RequestParam annotation in Spring MVC:
Description: @RequestParam
is an annotation in Spring MVC used to bind query parameters to method parameters.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processQueryString(@RequestParam String param) { // Logic to process the query parameter return ResponseEntity.ok("Received parameter: " + param); } }
Handling query parameters in Spring Controller:
Description: Query parameters are handled by using the @RequestParam
annotation in the method signature.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processQueryString(@RequestParam String param) { // Logic to process the query parameter return ResponseEntity.ok("Received parameter: " + param); } }
Spring MVC extract query parameters:
Description: Spring MVC automatically extracts query parameters when annotated with @RequestParam
.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processQueryString(@RequestParam String param) { // Logic to process the query parameter return ResponseEntity.ok("Received parameter: " + param); } }
How to pass query parameters in Spring MVC:
Description: Query parameters are passed in the URL, and you can capture them in the controller method using @RequestParam
.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processQueryString(@RequestParam String param) { // Logic to process the query parameter return ResponseEntity.ok("Received parameter: " + param); } }
Spring MVC @RequestParam vs @PathVariable:
Description: @RequestParam
is used for query parameters, while @PathVariable
is used for extracting values from the URI path.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processQueryString(@RequestParam String param, @PathVariable String pathVar) { // Logic to process query parameter and path variable return ResponseEntity.ok("Received parameter: " + param + ", Path variable: " + pathVar); } }
RequestMapping with multiple query parameters Spring MVC:
Description: You can handle multiple query parameters in a single method using multiple @RequestParam
annotations.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processMultipleQueryParams(@RequestParam String param1, @RequestParam String param2) { // Logic to process multiple query parameters return ResponseEntity.ok("Received parameters: " + param1 + ", " + param2); } }
Spring MVC request parameter mapping example:
Description: Request parameters can be mapped to method parameters using @RequestParam
.
Code snippet (Java):
@Controller @RequestMapping("/api") public class MyController { @GetMapping("/process") public ResponseEntity<String> processQueryString(@RequestParam("myParam") String param) { // Logic to process the query parameter return ResponseEntity.ok("Received parameter: " + param); } }