spring mvc @requestmapping best practice

Checked the official ref, found a million ways to do things. I guess I have 2 set of use cases. 1. return customized http response, basically I am responsible for filling in status code, response body(either XML or JSON or text). 2. return Model and View. view is a jsp page typically and fill in the view with data from modle.

My question is which is a better way to go? it possible to mix them together. In my first use set, is it possible to return a View? also is it possible to have both on them in one method. something like if A return customized http response, if B return ModelAndView.

Thanks!

Answers


The return value from any request handling method (i.e. on marked with the @RequestMapping annotation must either identify a view (that will generate the HTTP Response) or generate the HTTP Response itself.

Each handler method stands alone; by that I mean, you can return a view name from some handler methods and generate the HTTP Response in other handler methods.

Check out 15.3.2.3 Supported handler method arguments and return types in the Spring 3x reference document at http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/

As an option to generating the HTTP Response in the handler method, you could setup multiple view resolvers; one or more for normal view resolution (jsp pages, tiles, etc.) and one or more for "special" view resolution (XML, JSON, etc.). For the "special" views, you may want to create your own view class that extends org.springframework.web.servlet.view.AbstractView.


You can accomplish something similar to what you are describing using the ContentNegotiatingViewResolver, which can deal with serving different content based on a request, with no changes required to your @RequestMapping annotations, or in fact anything in you're controllers.

There are plenty of resources on how to use this method, including this and this


Need Your Help

Check component initilaze before add events in actionscript 3

flash actionscript-3 events

How can I check component (move clip or button) isInitilazed before add event (release or click) for it in actionscript 3?

for_each_zone used in linux system call

linux-kernel

in a new system call , i want to access each zone's statistics i.e in vm_stat[].

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.