I have many controllers in my Spring MVC web application and there is a param mandatoryParam
let's say which has to be present in all the requests to the web application.
Now I want to make that param-value available to all the methods in my web-layer and service-layer. How can I handle this scenario effectively?
Currently I am handling it in this way:
In this quick tutorial, we’ll explore Spring's @RequestParam annotation and its attributes. Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request. In this article, we introduce different types of @RequestMapping shortcuts for quick web development using traditional Spring MVC framework.
@RequestParam examples 1. Biding method parameter with web request parameter 2. Request parameter match method parameter name 3. Auto type conversion 4. @RequestParam with not mandatory parameters 5. @RequestParam with Default value 6. @RequestParam with List or array 7. @RequestParam with Map
Mapping a Multi-Value Parameter A single @RequestParam can have multiple values: 8. Conclusion In this article, we learned how to use @RequestParam. The full source code for the examples can be found in the GitHub project. with Spring?
Request parameter match method parameter name If both variable and request parameter name matches we don’t need to specify the parameter name in the @RequestParam annotation. In the example above, since** **both variable and request parameter name is ‘category’, binding is performed automatically. 3. Auto type conversion
@ControllerAdvice("net.myproject.mypackage")
public class MyControllerAdvice {
@ModelAttribute
public void myMethod(@RequestParam String mandatoryParam) {
// Use your mandatoryParam
}
}
myMethod()
will be called for every request to any controller in the net.myproject.mypackage
package. (Before Spring 4.0, you could not define a package. @ControllerAdvice
applied to all controllers).
See the Spring Reference for more details on @ModelAttribute
methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With