I am reading spring framework reference. When i read here. I found that spring mvc supports returning void types.And then i read some examples of using void.But these examples do not make me to understand when time to use void.Is there a better example of how to use it?
1 Answer
From the referenced document:
"... if the method handles the response itself (by writing the response content directly, declaring an argument of type ServletResponse / HttpServletResponse for that purpose) or if the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature)"
There are two conditions listed.
- If the method writes to the servletResponse directly. In this case, there is nothing for spring to do; a return value of
voidtells spring "I got this" and it does nothing with the response. - If the view name can be determined vai a
RequestToViewNameTranslator. In this situation, spring knows the view to return based on the request, so no return value is required.
3 Comments
Yuxino
Thanks very much.After listening to your explanation, I can understand that.
Yuxino
If don't using template engine,
void return type will be rarely used in actual applications.Right?DwB
Yes. I have only used void return type one time, when I wrote the content of the response myself.