让我们快速了解一下什么是 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 Concerns
。
Say 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)
好吧,您看到这只有在我的 web 应用程序中有明确的层分离才能实现。
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 output
。
Store all the objects
想要生成视图in Request Attribute
并access it in JSP
重新编译 Sevlet 并不意味着您不遵循 MVC,只是通过遵循 MVC,您的更改是最小的并且被分组在一个地方。
现在放弃 JSON 概念,让它简单明了 阅读本教程,它公平地解释了如何实现一个简洁的 MVC
Jsp MVC 教程。
一旦你掌握了,你总是可以添加更复杂的东西,比如 JSON、AJAX、异步请求等