For example, if a request succeeds, I will return a View ,if not, return a String indicating error message and set the content-type to either xml or json. And the JavaScript XHR call back methods开发者_StackOverflow中文版 will do the job of either redirecting to another page (View) or staying in the same page and showing error info.
Based on what I read, seems like I should use "void" as the return type for handler methods. Check this out: "void 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)."(Spring Framework reference).
What I dont understand is what " the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature)" means? Any anyone give me an example?
A cleaner solution is to have your normal controller method throw an exception on error, then have an @ExceptionHandler
method to catch it and return the error response.
@RequestMapping("/")
public View requestHandler() throws SomeException
{
// ...
}
@ExceptionHandler(SomeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public @ResponseBody String handleSomeException(SomeExcption ex)
{
return ex.getMessage(); // for example
}
A MappingJacksonHttpMessageConverter
used with @ResponseBody
will remove the need to access HttpServletResponse
directly to output JSON. Alternatively, use a MappingJacksonJsonView
and a Model
. The same can be done using an XML converter/view.
A RequestToViewNameTranslator
will (as one might guess) translate requests into view names, if no other view name is specified. See DefaultRequestToViewNameTranslator
for an example.
In your special case:
- if everyting is ok - then return a normal (jsp) view
- if an error occures - then print an error page
i would do it in a more standard way: The Request handler method "return" the view name or model and view. And if there is an error, then the request handler method throws an exception. Spring provides the functionality to handle exceptions be special view handlers.
What I dont understand is what " the view name is supposed to be implicitly determined through a RequestToViewNameTranslator (not declaring a response argument in the handler method signature)" means?
Quote from excelent article about Spring MVC:
One interesting case is what happens when a method does not specify a view (e.g. return type is void). In that case the convention is for the DispatcherServlet to re-use the path info of the request URI removing the leading slash and the extension. [...] Given this request handling method and a request mapping of "/accounts/show" we can expect the DispatcherServlet to fall back on the default view name of "accounts/show"
There also presents example. See http://www.infoq.com/articles/spring-2.5-ii-spring-mvc , chapter "Flexible Request Handling Method Signatures"
精彩评论