View: Using MVC mode in web applications
Today's commercial world, manufacturers have made great efforts in their services and production lines. The display show and customer interaction becomes a model centered on Internet. The companies are gradually establishing a web display platform to attract more new customers and diversify. And simplifying the interaction of existing customer groups, making B2B-based communications, making it easier in old services. Now, in fact, each bank provides online savings; each financial institution and brokerage line re-written its own trading system to allow global access, can trade with any place that has been connected to the Internet. In aviation, defense, pharmaceutical, data management and other industrial areas, Web interfaces have been added to their business model.
As web-based services and applications are rapidly growing, the Web application development field has improved improvement and development. Existing programming methods, design patterns, and code libraries have been reused (or rewritten) such that they are associated with web-based applications. Even the entire framework is created to reduce the development time, shorten the maintenance cycle, and simplify the code of the online application. Because the main purpose of the Model-View-Controller (MVC) design is to separate business logic layers and representing logic layers, because it is more exciting in the integration of various web programs, so it is preferred for web-based applications and services. Apache's Struts is actually a MVC implementation framework in Java. Fusebox is an implementation of another MVC, which is technically implemented in multiple web technology, such as ColdFusion, PHP, Java, ASP, and Lasso.
In this article, I will tell you how to use MVC in a web-based project. My Controller will dynamically call the Action Method, and redirect to the appropriate view (view), adjust the data stream between the logic and model (Model).
Because it is a web project, I will use servlets to do server-side processing and controller (Controller), JavaBean is a model (MODEL) layer, JSP as a presentation layer. If you are not familiar with Servlets, JSP, and J2EE components, please read the reference tutorial at the end of this article.
If you want to test my project or want to use it as a core program in a large program, you need to configure the Application Server for J2EE. Here I use free Tomcat, but in a corporate environment, you may use IBM WebSphere or Bea WebLogic as a business J2EE application service and EJB / JSP container. The installation of the application server exceeds the scope of this article, but the project's source program is to package the J2EE application WAR (Web compression) file with XML configuration file; therefore, you want to run it on a configured server, you What you need is to do it DROP to an appropriate location. Moreover, if you want to invest more points, don't write your own controller layer (Controller Layer), then you may also need to consider the Apache Struts framework, which is accompanied by a huge JSP tag library, you can provide you need Various types of functions.
project
To demonstrate how to use MVC mode in web applications, I created a simple project that contains some JSP views (JSP views) ---- in any browser ---- Some Assistant Beans, behavior Action Classes, a servlet controller class. The purpose of the project is to display weather information based on the user's postal coding or city name. The architecture of this project is to make any type of large online application easier and more easily modified. Enterprise-class applications typically have a database in the background, taken out dynamic data in the view, which is composed of the so-called three-layer architecture ---- client application, server processing, corporate data service. I have to store all data information into a HashMap object. Architecture
Main controller
Any MVC designed primary controller is responsible for coordinating the data stream between the model and the view layer, responding to the request and behavioral management model data. This is the right reason for Model-View-Controller in Web applications. If the controller is written, it boots the request data, calling behavior for a variety of number and type view. Therefore, you can easily apply the same controller to any item, then add the view you need.
I called my controller servlet, and added its definition in an XML configuration description file, which in my application server is called WeatherappWeb. Under the new web application (webApp).
Servlet has standard doget and dopost methods. However, it also has HashMap, which is used to dynamically store behavior objects, which is created from the key value passed from the JSP view.
I didn't use a property file to match the key value of the execution behavior or the JSP view used to display. Instead, I put the behavior key as a hidden variable (Hidden Varivables) into the JSP itself, indicating what the controller will do next? .
When JSP submits its own data to the controller, the main "magic" happens in the dopost method. I use the reflection API to instantiate the Action class, (first I check if it already exists). Then call a method through a parameter key value. The ACTIO N class is used by the controller Controller to perform various types of behavior. All behavioral classes implements an empty Actionin Terface that can be used to use reflection and instantiate them through the parameter key values returned by the JSP form. After the Action call returns, I will relocate to another view - this depends with the second key parameters passing through the Finally clause.
JSP view
This view is a simple HTML page that users can see in any web browser. They are generated by the JSP application server. The POST view has an HTML form where there is a submission behavior pointing to the controller servlet.