2013-02-26 100 views
0

我一直在阅读MVC,但真的不知道我是否清楚MVC的概念 最近开发了一个应用程序,我做的是 1)对jsp负载调用函数 2)使用AJAX调用servlet和servlet执行所有逻辑3)servlet调用一个java bean和一个java类来执行一些逻辑并返回结果 4)基于从类返回的结果我正在显示一个图像说如果结果是1然后图像A,如果0然后图像b 5)关于servlets POST方法我使用out.println() - >来写完整的输出 6)返回调用后jsp上的函数将设置由servle生成的输出所需的div的innetHTML牛逼MVC体系结构问题

说现在输出的servlet生产是表


实例名称|实例状态


现在,如果我想在一定的时间来改变显示该表说


实例状态|实例名称


做上述变化,我必须重新编译我的servlet和重新部署战争

真的是这样一个MVC? 和有人建议我使用包含数据的JSON存储对象作为JSON,然后将JSON对象返回给jsp ,并在jsp中使用此对象构造表!

对此的任何指针将有很大的帮助!

回答

0

是否需要重新部署通常取决于您的开发环境。如果您使用在进行更改时自动构建的IDE,并且服务器是从您正在使用的IDE运行的,则可能不需要重建战争。您可以随时尝试查看源代码以查看是否需要重新部署。后端代码通常需要重新部署。

0

基于获得的ajax响应。

您可以隐藏或显示您倾向于的图像。

如何获取图像链接,而不是out.print打印字节[]如果我没有错。

当你想重定向到另一个页面时,如何使用响应重定向从servlet本身重定向它。

0

让我们快速看看什么是MVC?

MVC(Mode-view-controller)顾名思义就是软件架构模式,它鼓励应用到have its Model Classes (i.e domain models/DTOs) views (i.e can be JSP, JSON etc) and controller (i.e Servlet) to be as modularized as possible so that it encourages re-usability, loose-coupling between the different layers and Seperation of Concerns

所以这个背后的关键想法是encourage Seperation of ConcernsSay i want to change the view from JSP to freemarker view , if MVC is tighly followed , i should be able to accomplish the change with minimum to no impact to Controller layer (i.e Servlets)

那么你看到,只有在我的webapp中有清晰的图层分离才能实现。 If i had just scattered all the functions without regard to MVC like having views generated from the Servlet, or making service level calls like accessing the DB directly from the Controller etc is bad because any change in the view or the Database layer will cause massive changes at the Servlet .

所以要回答你的问题your servlet should not directly produce the HTML outputStore all the objects将要生成视图in Request Attributeaccess it in JSP 并重新编译Sevlet并不意味着你不遵循MVC,只是按照MVC的变化是最小的,在一个地方进行分组。

现在下降JSON的概念,使之简单明了 通过这个教程,其中相当解释你如何实现一个整洁的MVC

Jsp MVC tutorial.

一旦你掌握,你可以随时添加更多复杂之类的东西JSON,AJAX异步请求等

+0

thnks所有帮助提供:) – 2013-02-28 17:16:38

+0

@ user2106092欢迎 – Sudhakar 2013-03-01 08:07:18