4

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 have a controller servlet "controller.java" which reads the servlet path i.e. request.getServletPath() to determine the action required
  • I have a number of different Enterprise Java Beans (EJB 3.1) which handle the business logic and which are called by my controller servlet depending on the action requested
  • I have a number of views which relate to different aspects of my application to which the request is forwarded (by the controller servlet) based on the action requested (i.e. request.getRequestDispatcher(url).forward(request, response);)

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

raven-king
  • 1,550
  • 2
  • 18
  • 40

3 Answers3

3

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.

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • Thanks for your reply... I agree with you that this is definately something I need to do. Could you tell me if this is something specific to a certain framework (a quick google brought back a few results specific to Spring-MVC) or whether I could implement this simply by having a separate servlets which responds to different URL patterns? – raven-king Dec 30 '10 at 16:58
  • I would suggest you to keep staying away from any frameworks. Mostly because you are learning the technologies now (as you mentioned in the question). And yes, you're right, separate modules by URL patterns/prefixes, which you map later to different Java packages. I would create a "master servlet", and would call it a dispatcher.. – yegor256 Dec 30 '10 at 19:18
0

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

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

chris
  • 3,820
  • 6
  • 36
  • 36