smwikipedia
smwikipedia

Reputation: 64401

How to determine the parameters of a Spring Controller method?

I see Spring MVC controller handler methods with various parameters. Such as:

So,

Upvotes: 3

Views: 1352

Answers (3)

Mudassar
Mudassar

Reputation: 3225

You will get the list of the supported types here Supported method argument types which go as the arguments of the handler methods.

Using java reflection, Spring will then resolve the arguments of your controller method. So in your case the Locale and model will automatically be passed to you. If you included another parameter, such as HttpSession, that will be passed to you.

Upvotes: 0

Ralph
Ralph

Reputation: 120851

See spring reference chapter: 21.3.3 Defining @RequestMapping handler methods, it list all supported parameters.

It does not have a fixed signature, because one has often to implement so much different controller, that all need different information. Of course you could do all most all extracting of information out of the HttpServletRequest Object by your own, but this is not the Spring way: the Spring-Way is more that you using highlevel abstraction to implement the business logic, and let spring do the lowlevel technical stuff.

But the most important point about using the flexible annotation bases approach over an interface- or inheritance-based-approach is, that you can have multiple request handler methods in one controller class. (While with an interface you would need almost one class per handler.)

Upvotes: 1

Bohuslav Burghardt
Bohuslav Burghardt

Reputation: 34816

Spring controller method signatures are very flexible and support number of parameters. They are listed in the documentation.

Regarding your second question, there is no fixed signature exactly for the reason of providing flexibility when writing controller methods.

The individual parameters are resolved using implementations of HandlerMethodArgumentResolver interface. For instance if you have parameter annotated with @PathVariable parameter, its value will be resolved by PathVariableMethodArgumentResolver.

You can even create your own implementation of HandlerMethodArgumentResolver to allow some custom parameter types in your controller method signatures.


Historically there used to be controllers with fixed signatures in Spring. They would implement the AbstractCommandController interface for instance. Such controller would implement following method:

ModelAndView handle(HttpServletRequest request, HttpServletResponse response,
        Object command, BindException errors)

However Spring has evolved since then and this approach was deprecated in favor of annotation based controllers in Spring 3.

Upvotes: 1

Related Questions