Explain MVC architecture relating to J2EE ?
This is also a very popular interview question. MVC stands for Model-View-Controller architecture. MVC is a software design pattern. It divides the functionality of displaying and maintaining of the data to minimize the degree of coupling (loose coupling) between components. This is a typical multi-tier structure designing ideas. There are three categories in the following:
The model is the principal part of application program. The model is another name for the application logic layer. It expresses business data, it is clearly said that one model is a record in database. It is independent of the data form, in other words one model may provide the data for many views, in this way it reduces the repeated code for our application program.
The view is an interrelated part of User Interface (UI) in application program and a seeing and exchanging interface by users. One of advantages is that it can process many different views for your application program using MVC. Actually does not have the true processing to take place in the view, and the view is only regarded as a means of output data and user operation?
The controller controls UI data displaying and updates the model object state according to the users input. The controller accepts the user input and calls the model and view to complete the user demand. So when we click the hyperlinks in the web page and send HTML table list, the controller itself does not have any output and make any processing. It only receives the request and determines calling which model to deal with the request, and it confirms to use which view to display the returning data of the model processing. There is the relationship of the model, the view and the controller, as shown in Fig.
Though MVC comes in different flavors, control flow is generally as follows:
1. The user interacts with the user interface in some way (for example, presses a mouse button). The controller handles the input event from the user interface, often via a registered handler or callback and converts the event into appropriate user action, understandable for the model.
2. The controller notifies the model of the user action, possibly resulting in a change in the model’s state. (For example, the controller updates the user’s shopping cart.) 4. A view queries the model in order to generate an appropriate user interface (for example, the view lists the shopping carts contents). Note that the view gets its own data from the model. The controller may (in some implementations) issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) which require a screen update. 5. The user interface waits for further user interactions, which restarts the cycle. Some implementations such as the W3C XForms also use the concept of a dependency graph to automate the updating of views when data in the model changes.
3. By decoupling models and views, MVC helps to reduce the complexity in architectural design and to increase flexibility and reuse of code.
4. Web server is an important part of J2EE architecture. The main technologies are Servlet and JSP; its function implements UI. Because the essential of JSP and Servlet is a small Java program on the server side, it can exchange Java Applet and HTML with the client. Not only is implementing speed fast and is UI generation flexible, but also is the security very good, at the same time, it can realize some simple application logic. Following diagram is presenting the flow of MVC architecture.
The component technology of JSP, Servlet, EJB etc. based on J2EE platform and MVC design pattern can simplify the developing process, improve the software performance. Moreover, Based on the development architecture composed by the Struts and the Hibernate, it passes the data by the value object which the layers corresponding to, and strictly controls the visit to the persistence layer by the users. In this way, it can protect the business data effectively. Besides, it is useful to the upper debug and maintenance by separating the business logic and the data expression, and also by separating the data in different layers.