Question about configuring the Spring controller?

advertisements

I am using Spring MVC to develop a Java webapp. I have a setup as follows: 2 pages of the site are responsible for rendering similar content. One of the pages simply renders the items slightly differently (and hence would need a different view), and pulls the data slightly differently (with a limit on the query instead of pulling all the items).

My question is: would the best approach for this be to implement 2 separate controllers to handle each of these pages, or would it be better to use a single controller? If the answer is to use a single controller, how can I find out from inside the controller from what page the request came from (so that I can call the appropriate service and view)? Thanks


With Spring 2.5+ annotated controllers, the difference between having one controller or two controllers is not especially relevant.

For example, say you have a controller class like this:

@Controller
public class MyController {

   @RequestMapping ("/pageA");
   public String handlePageA() {
      .. do stuff
   }

   @RequestMapping ("/pageB");
   public String handlePageB() {
      .. do stuff
   }
}

It should be obvious how this works. You get the benefit of one controller, with two handler methods, one for each "page".

There really is no reason to use pre-2.5 controllers any more. You can use the new style alongside the old one, so even legacy pre-2.5 apps can use the new style after an upgrade.