raven-king
raven-king

Reputation: 1550

Architecture of AJAX MVC application

I've written the bare bones of my application using the MVC pattern. I don't currently have any AJAX functionality in my application but I was looking for suggestions on how I would change the architecture of my application to achieve this, to that end I'll try my best to describe my current architecture:

I understand that the current architecture could support AJAX functionality (by matching a pattern from my "controller.java" servlet) but I'm getting to the point where I have a huge number of actions supported by my controller and it's getting messy.

Does anybody have any suggestions? Is there a standard pattern for doing this? I'm trying to stay free of any frameworks just now as I'm a relative beginner! :-)

Thanks

Upvotes: 4

Views: 1360

Answers (3)

chris
chris

Reputation: 3858

Same idea with BalusC.

We have an MVC app that runs by itself. Now to add AJAX functionality we added JQuery and used jqGrid in the presentation layer. It communicates with the backend via AJAX. If we remove the JQuery and jqGrid, we still have a fully running MVC app.

I've put a demo of that at http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration. Here we integrated Spring MVC 3 and jqGrid/JQuery

Upvotes: 0

BalusC
BalusC

Reputation: 1108567

You can check for ajax requests as follows:

boolean ajax = "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));

and then handle the kind of response accordingly. I.e. returning the view ID which is to be used in a forward or redirect, or returning some JSON which is then to be written to response body, or returning a special View object which contains this kind of information. Given this basic MVC example, it should not be that hard to expand it with ajax support.

Upvotes: 0

yegor256
yegor256

Reputation: 105043

If your controller supports a huge number of actions - it's where you need refactoring. In general your architecture looks correct, if the number of actions is reasonable (up to 10, I would say) per each controller.

One possible way of refactoring is to group controllers into modules.

Upvotes: 3

Related Questions