Model-View-Controller, what every part really does

2019-06-05 16:34发布

问题:

Hi guys recently I've started exploring Spring MVC, as currently I am involved in a project using it. Before that I checked out Struts 2 framework. However, In my opinion the two frame work has a different definition and implementation of the MVC design pattern, AngularJS, as a client side MVC framework has its own too. I will explain what differences I've noticed between Spring MVC and Struts 2.

Spring : (quoting from Spring in Action )

1- Controller: In Spring, the controller is a component that process a request, does some logic (ideally this logic should be extracted to some service or business logic class) and returns data back.

"The DispatcherServlet’s job is to send the request on to a Spring MVC controller. A controller is a Spring component that processes the request. But a typical application may have several controllers and DispatcherServlet needs some help deciding which controller to send the request to. So the DispatcherServlet consults one or more handler mappings to figure out where the request’s next stop will be. The han- dler mapping will pay particular attention to the URL carried by the request when making its decision." p166

2- Model: is some information that needs to be carried back to the user and displayed in the browser.

"The logic performed by a controller often results in some information that needs to be carried back to the user and displayed in the browser. This information is referred to as the model."

3- View: This is the most obvious one its the HTML+CSS combined with model information.

Now lets us see how Struts 2 does the MVC

Struts2: (quoting from Struts 2 in Action )

1- Controller : The controller is a component that maps a url to an appropriate action,actions implement models in struts ?! ( in spring this is not what controller does ). Struts has only one controller which is FilterDispatcher. in Spring this is done by DispatcherServlet’s which they don't call controller !

"The controller’s job is to map requests to actions"

"The role of the controller is played by the Struts 2 FilterDispatcher" p13

2- Model:The model, in struts, is a component that hold business log and some data model together this make the application state.

"model is implemented by the Struts 2 action component" p13

"In more technical terms, the model is the internal state of the application. This state is composed of both the data model and the business logic" p14

so the model in struts is not only data model like User, Contract, Order .... it also contains the logic of the application.

Here are the two diagram of string and struts 2 as represented in these two books: Spring MVC

Struts 2

Now, Which MVC is the correct MVC ?

Who the front controller MVC is related to each other ?

Thanks for answers in advance.

回答1:

I think of it this way.

Model: stores and retrieve data; enforces policies that must be consistent for all applications that use it, known as "business logic". (Example: age must > 18.)

View: presents data to the user; provides a mechanism for the user to interact with the system.

Controller: mediates between the Model and View; enforces policies specific to this application (sanitising input, for example), sometimes confused with "business logic". (Example: age must be a string with only numbers, and here's how to parse it.)

I see two common varieties of MVC, one for desktop UIs and one for web UIs.

Web MVC: Controller handles requests, updates and/or queries Model, chooses View template, merges Model with View template.

Desktop MVC: Controller interprets user gesture, updates and/or queries Model; Model fires "I've changed!" events; View subscribes to "I've changed!" events to update themselves.

Spring's description of Controller confuses it with what Martin Fowler calls a "Transaction Script" in his book Patterns of Enterprise Application Architecture. I understand this, because we often implement Transaction Scripts as Controller-type actions. Ironically, Struts encourages (or at least encouraged) this more than Spring did.

"Actions implement Models in Struts" sounds like utter insanity. I can only assume that it's a mistake.

Finally, the Front Controller is a central request handler that dispatches requests to a more specific controller. In desktop apps, we can connect "TransferFundsController" directly to the "TransferFundsAction" button, so we don't need a Front Controller. In web apps, we have a single point of entry for all requests, and we have a lot of common behavior (parsing request parameters, for example), so we often want a Front Controller to do all that.

I hope this helps.

Further Reading:

Applications Programming in Smalltalk-80(TM): How to use Model-View-Controller (MVC) http://st-www.cs.illinois.edu/users/smarch/st-docs/mvc.html

http://c2.com/cgi/wiki?ModelViewControllerHistory



回答2:

according to this wiki link. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller. MVC is a design pattern, where Model holds business data, validation rules, not much of business logic (as far as i know).

2- Model:The model, in struts, is a component that hold business log and some data model together this make the application state.

model is plain pojos of setters and getter. i have never used business logic in model data, apart from validation rules.

Controller (spring)/Action class (struts) are controllers which controls the model data and views. i.e it is central point which receives the model , update it upon calling service layer and response with appropriate view based on configuration.

view - is nothing but the final output the user will see upon success/ failure response.

Dispatcher servlet and Filterdispatchers are url mapping handlers which identifies its controller (injects model data and handlers) and receives the view inturn.

on ground both spring and struts adhere to basics of what Controller, Model and View supposed to do. to summarize both spring and struts uses MVC pattern where as implementation logic differs between these two frameworks.



回答3:

I can tell you how I use the Struts's MVC structure in my web application:

View: JSP + CSS + some scripting you might need

Controller: Here I do the HTTP GET/POST, as well as the interpretation of every action through the use of codes. For example, the logout button has code 1, which is mapped from JSP to Java through a Form class. This value is sent from the View to the Controller through a JS script.

Model: Here's where the magic happens. The Model is responsible for processing the whole business logic. Whatever stuff you need to process, whatever calculations or data transformations you might need, this is where it all goes down.

This is a very practical example of what the MVC does based on my own experience, which I think is easier to understand than reading a bunch of theory about it. But what's the point of all this? To do as much processing as possible on the server side. By adopting this MVC structure, you'll avoid programming possibly complex functions in JS and do them in good old Java instead. I found this to be a big advantage, because I don't really like JS.

That being said, I believe your Struts 2 diagram is the correct MVC.

I'm sure there's a lot more to be said about the MVC and its benefits, but I hope that my answer is helpful to you anyway.