Combustion007
Combustion007

Reputation: 474

Spring MVC 3.0 Controller and resolvers

I would highly appreciate it if someone can share their thoughts on this. I am new to Spring, but not new to MVC. However, my exposure with MVC has mostly been on the front-end side of things.

Lets say if I have a dynamic site which employs SpringMVC Framework 3.0, and site has 5 links such as:

HOME | FAMILY | COMEDY | HORROR | ACTION

Each of these links would query a database and display information. If I was to be doing this for front-end via a MVC framework, I would have five different controllers, but I came across a question here (Stackoverflow) which talks about having just one controller and then numerous resolvers. I would like to know what is the correct approach?

Here is the link to the mentioned question: Spring controller setup question?

Upvotes: 1

Views: 240

Answers (3)

nickdos
nickdos

Reputation: 8414

As Jaanas said, it really depends on how you are retrieving the data for each link and whether the resulting pages are all based on the same data model and templete (JSP) or are completely different to each other.

I tend to use a separate controller for "home" but if those other pages are simply querying a database then, I'd start serving those from a single controller.

Its also a good idea to try and think of your pages in terms of REST principles. Once you start seeing the links in terms of REST URIs, then the decision of whether the various URIs go in the same or separate controllers is obvious. E.g. if the URI for "family" is /movie/genre/family, then it becomes clear that all 4 of those links should go in the GenreController class (or a MovieController class, with a /genre method capturing the category with a @PathVariable annotation).

Upvotes: 2

Naresh J
Naresh J

Reputation: 2137

The number of controllers in your application depends on the scope of your project. If you have fixed requirement and the number of operations are limited then you can use single controller which will handle your all request.

But if your requirements are not fixed or going to increment with your application version, you must use separate controller for separate module. So that in future you can easily manage your application code.

For better code maintenance, I will prefer to use separate controller for each module.

Upvotes: 1

Jaanus
Jaanus

Reputation: 16541

It depends on how much logic you have behind each link. If you have only one mapping in each controller, you could use single controller.

That said, I still prefer having separate controllers, when doing only small project like yours.

On the other hand, if your project expands, you could end up having absurd amount of controller, that don't do much. In that case you could group your controllers from start, so grouping similar controllers, that have almost equal logical side.

Upvotes: 1

Related Questions