简体   繁体   English

AJAX MVC应用程序的体系结构

[英]Architecture of AJAX MVC application

I've written the bare bones of my application using the MVC pattern. 我使用MVC模式编写了我的应用程序的骨架。 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: 我目前在我的应用程序中没有任何AJAX功能,但我正在寻找有关如何更改应用程序架构以实现此目的的建议,为此我将尽力描述我当前的架构:

  • I have a controller servlet "controller.java" which reads the servlet path ie request.getServletPath() to determine the action required 我有一个控制器servlet“controller.java”,它读取servlet路径,即request.getServletPath()来确定所需的操作
  • 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 我有许多不同的Enterprise Java Bean(EJB 3.1),它们处理业务逻辑,并由我的控制器servlet根据请求的操作调用
  • 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 (ie request.getRequestDispatcher(url).forward(request, response); ) 我有许多视图,它们涉及我的应用程序的不同方面,请求被转发(由控制器servlet)基于请求的操作(即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. 我知道当前的架构可以支持AJAX功能(通过匹配我的“controller.java”servlet中的模式)但是我已经到了我的控制器支持大量操作并且它变得混乱的程度。

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 谢谢

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. 一般来说,如果每个控制器的操作数量合理(最多10个,我会说),那么您的体系结构看起来是正确的。

One possible way of refactoring is to group controllers into modules . 一种可能的重构方法是将控制器分组到模块中

You can check for ajax requests as follows: 您可以按如下方式检查ajax请求:

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

and then handle the kind of response accordingly. 然后相应地处理这种响应。 Ie 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. 即返回将在转发或重定向中使用的视图ID,或返回一些JSON,然后将其写入响应主体,或返回包含此类信息的特殊View对象。 Given this basic MVC example , it should not be that hard to expand it with ajax support. 鉴于这个基本的MVC示例 ,使用ajax支持扩展它应该不难。

Same idea with BalusC. 与BalusC相同的想法。

We have an MVC app that runs by itself. 我们有一个独立运行的MVC应用程序。 Now to add AJAX functionality we added JQuery and used jqGrid in the presentation layer. 现在要添加AJAX功能,我们添加了JQuery并在表示层中使用了jqGrid。 It communicates with the backend via AJAX. 它通过AJAX与后端通信。 If we remove the JQuery and jqGrid, we still have a fully running MVC app. 如果我们删除JQuery和jqGrid,我们仍然有一个完全运行的MVC应用程序。

I've put a demo of that at http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration . 我在http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration上进行了演示。 Here we integrated Spring MVC 3 and jqGrid/JQuery 这里我们集成了Spring MVC 3和jqGrid / JQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM