Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring Basics Using the MVC Architecture Add a CategoryController

Thossaporn Itngam
Thossaporn Itngam
2,348 Points

How does ModelMap, Model can inject to parameter ?

For example i saw some parameter like this.

@RequestMapping("/hello") public String hello(Model model){ // do something return "hello"; }

as you see model in parameter , i didn't see @autowired annotation but it can inject to this method.

i would like to know how to inject parameter without @autowired. please explain topic to me, i will search

Thanks.

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

I think it all comes from @RequestMapping annotation.

When you use @RequestMapping annotation, you are injecting whole bunch of pre-filled parameters, such as

Quoting the Note Section in JavaDocs

Request and/or response objects (Servlet API or Portlet API). You may choose any specific request/response type, e.g. ServletRequest / HttpServletRequest or PortletRequest / ActionRequest / RenderRequest. Note that in the Portlet case, an explicitly declared action/render argument is also used for mapping specific request types onto a handler method (in case of no other information given that differentiates between action and render requests).

Session object (Servlet API or Portlet API): either HttpSession or PortletSession. An argument of this type will enforce the presence of a corresponding session. As a consequence, such an argument will never be null. Note that session access may not be thread-safe, in particular in a Servlet environment: Consider switching the "synchronizeOnSession" flag to "true" if multiple requests are allowed to access a session concurrently.

WebRequest or NativeWebRequest. Allows for generic request parameter access as well as request/session attribute access, without ties to the native Servlet/Portlet API.

Locale for the current request locale (determined by the most specific locale resolver available, i.e. the configured LocaleResolver in a Servlet environment and the portal locale in a Portlet environment).

InputStream / Reader for access to the request's content. This will be the raw InputStream/Reader as exposed by the Servlet/Portlet API.

OutputStream / Writer for generating the response's content. This will be the raw OutputStream/Writer as exposed by the Servlet/Portlet API. HttpMethod for the HTTP request method

@PathVariable annotated parameters (Servlet-only) for access to URI template values (i.e. /hotels/{hotel}). Variable values will be converted to the declared method argument type. By default, the URI template will match against the regular expression [^.]* (i.e. any character other than period), but this can be changed by specifying another regular expression, like so: /hotels/{hotel:\d+}. Additionally, @PathVariable can be used on a Map<String, String> to gain access to all URI template variables.

@MatrixVariable annotated parameters (Servlet-only) for access to name-value pairs located in URI path segments. Matrix variables must be represented with a URI template variable. For example /hotels/{hotel} where the incoming URL may be "/hotels/42;q=1". Additionally, @MatrixVariable can be used on a Map<String, String> to gain access to all matrix variables in the URL or to those in a specific path variable.

@RequestParam annotated parameters for access to specific Servlet/Portlet request parameters. Parameter values will be converted to the declared method argument type. Additionally, @RequestParam can be used on a Map<String, String> or MultiValueMap<String, String> method parameter to gain access to all request parameters.

@RequestHeader annotated parameters for access to specific Servlet/Portlet request HTTP headers. Parameter values will be converted to the declared method argument type. Additionally, @RequestHeader can be used on a Map<String, String>, MultiValueMap<String, String>, or HttpHeaders method parameter to gain access to all request headers. @RequestBody annotated parameters (Servlet-only) for access to the Servlet request HTTP contents. The request stream will be converted to the declared method argument type using message converters. Such parameters may optionally be annotated with @Valid and also support access to validation results through an Errors argument. Instead a MethodArgumentNotValidException exception is raised.

@RequestPart annotated parameters (Servlet-only, @MVC 3.1-only) for access to the content of a part of "multipart/form-data" request. The request part stream will be converted to the declared method argument type using message converters. Such parameters may optionally be annotated with @Valid and support access to validation results through a Errors argument. Instead a MethodArgumentNotValidException exception is raised.

@SessionAttribute annotated parameters for access to existing, permanent session attributes (e.g. user authentication object) as opposed to model attributes temporarily stored in the session as part of a controller workflow via SessionAttributes. @RequestAttribute annotated parameters for access to request attributes. HttpEntity<?> parameters (Servlet-only) for access to the Servlet request HTTP headers and contents. The request stream will be converted to the entity body using message converters.

Map / Model / ModelMap for enriching the implicit model that will be exposed to the web view.

RedirectAttributes (Servlet-only, @MVC 3.1-only) to specify the exact set of attributes to use in case of a redirect and also to add flash attributes (attributes stored temporarily on the server-side to make them available to the request after the redirect). RedirectAttributes is used instead of the implicit model if the method returns a "redirect:" prefixed view name or RedirectView.

Command/form objects to bind parameters to: as bean properties or fields, with customizable type conversion, depending on InitBinder methods and/or the HandlerAdapter configuration - see the "webBindingInitializer" property on RequestMappingHandlerMethodAdapter. Such command objects along with their validation results will be exposed as model attributes, by default using the non-qualified command class name in property notation (e.g. "orderAddress" for type "mypackage.OrderAddress"). Specify a parameter-level @ModelAttribute annotation for declaring a specific model attribute name.

Errors / BindingResult validation results for a preceding command/form object (the immediate preceding argument). SessionStatus status handle for marking form processing as complete (triggering the cleanup of session attributes that have been indicated by the @SessionAttributes annotation at the handler type level). UriComponentsBuilder (Servlet-only, @MVC 3.1-only) for preparing a URL relative to the current request's host, port, scheme, context path, and the literal part of the servlet mapping.

As you can see your Map/Model is exactly inside...

And when you add parameter to your method annotated with @RequestMapping you basically inject, or @Autowire one of those objects...

Does that make sense ?