1

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 1

1

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.

  1. If the method writes to the servletResponse directly. In this case, there is nothing for spring to do; a return value of void tells spring "I got this" and it does nothing with the response.
  2. 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.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much.After listening to your explanation, I can understand that.
If don't using template engine, void return type will be rarely used in actual applications.Right?
Yes. I have only used void return type one time, when I wrote the content of the response myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.