Spring MVC and Web Layer
August 19, 2021
M05 Q09 What are some of the parameter types for a controller method?
Spring Framework supports many parameter types for controllers.
WebRequest, NativeWebRequest
β Access to HTTP request details, parameters, also request and session attributes, without direct use of the Servlet API
javax.servlet.ServletRequest
- object to provide client request information, allows access to parameters, attributes and other request details without direct use of Spring Interface
javax.servlet.ServletResponse
β object created by servlet container, passed to service method of servlet, used by servlet to send a response to the client
javax.servlet.http.HttpSession
β allows access to session information and attributes, also enforces HTTP session for request. This example needs a cookie to writesessionId
into the cookie.
javax.servlet.http.PushBuilder
- Servlet 4.0 push builder API for programmatic HTTP/2 resource pushes, allows resources to be delivered in advance by the server, resulting in a faster load time
java.security.Principal
- currently authenticated user
HttpMethod
β HTTP method used for request, one of GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
java.util.Locale
- request locale, determined by the most specific LocaleResolver available
java.util.TimeZone
+java.time.ZoneId
- time zone associated with the current request, as determined by aLocaleContextResolver
.
java.io.InputStream
,java.io.Reader
β allows access to raw request body as exposed by the Servlet API
java.io.OutputStream
,java.io.Writer
β allows to create raw response as exposed by the Servlet APIHttpEntity
- container object that exposes request headers and body, body is converted with usage ofHttpMessageConverter
java.util.Map
,org.springframework.ui.Model
,org.springframework.ui.ModelMap
β used to expose data to templates as part of view rendering
RedirectAttributes
β specify attributes to use in case of redirect, regular attributes will be added to query string and flash attributes will be kept temporarily until end of request, flash attributes are kept typically in the session and are removed immediately after request is completed
Errors
,BindingResult
β used to gain access to form validation and binding data results, can be used with@ModelAttribute
,@RequestBody
or@RequestPart
argument,Errors
andBindingResult
argument must be declared immediately after the validated method argument
SessionStatus
+ class-level@SessionAttributes
β useful for multi step form processing,@SessionAttributes
allows to keep@ModelAttribute
objects between requests and SessionStatus allows to clean session variables when form processing is done
UriComponentsBuilder
β used to build URLs relative to currentscheme
,host
,port
,contextPath
etc.
- Any other argument β if a method argument is not matched against types defined before, and it is a simply type, it is treated as
@RequestParam
, if it is a complex type, it is treated as@ModelAttribute