Java Web Three-tier architecture detailed

Source: Internet
Author: User
Tags connection pooling

Java three-tier architecture ssh

A spring2.5+hibernate3.2+struts2.0 combo framework that uses spring 's IOC to manage all The bean applications, including the struts2 action, Give full play to the advantages of the spring lightweight framework.

Abstract: for current Web application development problems, combined with the popular open source framework spring, struts, and hibernate, a lightweight solution for developing Java EE Web applications is proposed to help developers build clear structures in the short term , reusable and easy to maintain Web applications. Also, the case illustrates how this scenario can be applied to a real project.
Key Words: Java EE MVC Struts Spring Hibernate

The development of a large enterprise-level Web application system often requires a good software architecture, facilitates collaborative development, and extends the upgrade, while traditional development models do not meet these requirements well. Based on the current problems of web application development, this paper presents a lightweight solution to develop Java EE Enterprise-class Web application, combining with the popular open source framework ssh (Spring, Struts, Hibernate). And through the case to specify how to apply this scenario to the actual project.
1 Framework Technology
The famous software master Ralph Johnson defines the framework as follows: A framework is a reusable design that is part of an entire system or system, consisting of a set of abstract classes and their interactions between instances [1].
Frameworks generally have plug-and-play reusability, proven stability, and good teamwork. The complex multi-layered structure of the Java EE determines that the large-scale EE project needs to use frame and design pattern to control software quality. At present, there are some commercial and open-source Java EE-based application frameworks in the market, among which the mainstream framework technologies are: the Struts framework based on the MVC pattern and the spring framework based on the IOC model, and the object/relational mapping framework hibernate.
1.1 Presentation Layer Frame struts
Struts is an MVC framework implemented on the basis of JSP Model2, which is mainly divided into three parts, model, view (Viewer) and controller, whose main design idea is to decouple the performance logic and the business logic through the controller, To improve the maintainability, scalability and reusability of the system [2]. The struts framework is shown in Architecture 1.


The following is an analysis of the MVC components in the Struts framework in the architecture diagram shown in Figure 1.
(1) View: The view part consists mainly of JSP pages, where there is no process logic, business logic, and model information, only tags. Struts itself contains a set of tag libraries (TAGLIB), which is one of the essence of struts, and the flexibility to use them simplifies the code of JSP pages and improves development efficiency.
(2) Controller: Controllers in struts are primarily the actionservlet they provide. Actionservlet receives all requests from the client and transfers control to the appropriate action object based on the definition in the configuration file (Struts-config.xml).
(3) Model: Struts does not define the implementation of the specific model layer, the model layer is usually closely related to the business logic, there are persistent requirements. There are some excellent tools for the development of the model layer in both the business and open source worlds.
1.2 Business Logic Layer Framework Spring
Spring is a powerful lightweight framework that solves many common problems in the development of Java EE and can replace EJB technology. The lightweight described here refers to the spring framework itself, not to the fact that spring can only be used for lightweight application development. The lightness of spring is reflected in the infrastructure of its framework and the support and assembly capabilities of other application tools. Spring allows program developers to reduce the risk between different levels of technology, compared to the giant EJB.
The core of the spring framework is the control rollover IOC (inversion of control)/Dependency Injection di (dependence injection) mechanism. IOC refers to the relationship between control components in a container (where a container is a standardized runtime environment that provides specific services and technical support for a component) rather than a traditional implementation that is directly manipulated by program code, which transfers control from program code to external containers, called "flipping" [3]. Di is a more figurative interpretation of the IOC, that is, the container dynamically injects dependencies (such as construction parameters, construction objects, or interfaces) into the component during runtime [3]. Spring uses the mechanism of setting value injection (using setter methods to implement dependencies) and constructing sub-injection (implementing dependencies in the construction method) to create an IOC container that can construct components through the configuration file management of a collaborative object. This way, you do not need to write factory patterns, singleton schemas, or other constructs to get the required business components directly from the container. The spring framework is shown in Structure 2.


The spring framework consists of seven well-defined modules, and each module or component can exist individually or in conjunction with one or more modules. Spring Core container, an IOC container for managing business components, is the core of spring applications, and Spring DAO and Spring ORM provide not only abstract modules for data access, but also integration of Hibernate, Jdo and Ibatis and other popular object-relational mapping framework support module, and provide buffer connection pool, transaction processing and other important service functions, to ensure the performance of the system and data integrity; The Sprnig Web module provides some abstract encapsulation of Web applications that enable struts, Web frameworks such as WebWork and spring are integrated into a solution that works for you.
The spring framework can be a one-stop solution for enterprise-class applications, and it is also a modular framework that allows developers to freely pick and choose the right modules for their application development. Spring frame is a loosely coupled framework in which part of the coupling is designed to be minimal, depending on the needs of the developer at each level.
1.3 Data Persistence Layer Framework Hibernate
The O/R mapping technology is created to solve the mismatch between relational database and object-oriented programming. Hibernate is currently the most popular O/R mapping framework, which makes an automatic mapping between the relational database and the Java object, allowing programmers to perform operations on the database in a very simple way. Hibernate works as shown in principle 3.


Hibernate enables programmers to focus on the development of OO programs by masking the underlying database operations to programmers through the encapsulation of JDBC, which helps to improve development efficiency. [4] What the programmer needs to do to access the database is to compile an XML mapping file for the persisted object.
Changes to the underlying database require simply changing the initialization configuration file (Hibernate.cfg.xml or hibernate.properties) without impacting the application.
Hibernate has its own object-oriented query language HQL,HQL powerful, supporting most of the current mainstream databases, such as Oracle, DB2, MySQL, Microsoft SQL Server, etc., is currently the most widely used O/r mapping tool. Hibernate provides the underlying support for rapid development of applications.
2 design and implementation of Web application model based on SSH combo framework
2.1 New Java Framework for SSH
The previous three kinds of framework technology based on Java EE were analyzed, and the traditional Java EE Web development model was improved by integrating the above three framework technologies to form a new and lightweight Java EE architecture.
The system framework of the integrated SSH framework is shown in figure 4, and the system is divided into four layers: the presentation layer, the business logic layer, the data persistence layer, and the domain module layer. It uses struts as the overall infrastructure of the system, is responsible for the separation of MVC, and in the model part of the Struts framework, supports the persistence layer with the Hibernate framework, which is supported by the business layer with spring. The concrete approach is: using object-oriented analysis method to put forward some models according to requirements, the implementation of these models as basic Java objects, and then write the basic DAO interface, and give hibernate DAO implementation, using Hibernate architecture implemented The DAO class implements the transformation and access between the Java class and the database, and finally the business logic is completed by spring.


The basic business process of the system is: in the presentation layer, first through the JSP page to implement the interactive interface, responsible for transmitting requests (request) and receiving response (Response), and then struts according to the configuration file (Struts-config.xml) Delegate the request received by Actionservlet to the appropriate action handler. In the business layer, the Spring IOC container that manages the serviced component is responsible for delivering the business model component to the action and the component's collaboration Object data Processing (DAO) component to complete the business logic and to provide container components such as transaction processing, buffer pooling to improve system performance and ensure data integrity. In the persistence layer, it relies on Hibernate's object mapping and database interaction, processing the data requested by the DAO component, and returning the processing results.
The above development model not only realizes the complete separation of the view, controller and model, but also realizes the separation of the business logic layer and the persistence layer. This way, no matter how the front-end changes, the model layer requires little change, and the database changes will not affect the front end, greatly improving the reusability of the system. And because of the small coupling between different layers, it is advantageous to the team members to work in parallel, greatly improving the development efficiency.
2.2 Implementation of Web application system based on SSH framework
The following is an actual system that shows how to develop a Web application based on the SSH framework . The system is for a communications company operating department developed a question-and-answer system, functions similar to Baidu know and Sina love to ask. Due to the many modules of the system, the following is a user management module as an example to illustrate the implementation process of the system, and will follow the data persistence layer, the business logic layer, the presentation layer of the order of the system to explain the construction process.
(1) Data persistence layer
The data persistence layer consists of a Java object persistence class and a data Access object (DAO). Each database table corresponds to a persistent object, which gives developers the convenience of using OO thought design and development, but also shielding the specific database and specific data tables, fields, eliminating the hard coding of database operations in the reuse of the drawbacks. Some of the structure of the User information table is shown in table 1.


Hibernate relates objects (object) to relational data (relational) through a mapping (Mapping) file, so you need to write the Java persistence classes and corresponding mapping files that correspond to the database tables. With the Java persistence class, data access classes can be implemented on this basis. In the spring framework, data access classes can inherit from the auxiliary class Hibernatedaosupport, which greatly facilitates the use of the Hibernate framework in spring, and the corresponding sections of the code are as follows:
public class Userdao
Extends Hibernatedaosupport {
public int Add (user user) {
Return Integer.parseint (This.gethibernatetemplate (). Save (user). ToString ());
}
Public List FindAll () {
Return This.gethibernatetemplate (). Loadall (User.class);
}
}
The specific hibernate data source, session factory, transaction management, buffer connection pool and other functions are provided by the spring container of the business layer.
(2) Business logic Layer
The business logic layer is supported by the spring framework and provides a service component that handles business logic. Developers need to model business objects, abstract business models, and encapsulate them in model components. Because the data persistence layer implements the Java Persistence class and encapsulates the data Access Object (DAO), it is convenient to invoke the DAO component in the model component to access the data. Spring's IOC container is responsible for the unified management of the model and DAO components, as well as the service components provided by spring, such as transaction processing, buffer connection pooling, and so on.
In the user Management module, the user model UserService class is created through business modeling, which encapsulates the functions of user's rights management and integral management. The UserService class implements operations on user data by invoking the data access class Userdao. The relationships of these components are linked by configuring the Applicationcontext.xml of the spring framework, and the main contents of the configuration file are as follows:

   

(3) Presentation layer
The presentation layer combines the Taglib library processing display with JSP and struts, using Actionservlet to map the request (*.do) to the appropriate action and invoke the service component of the business logic by the action. It then jumps to the response page specified by the ForWord object based on the processing results.
The deployment of the orchestration is done by Struts-config.xml. The following is an example of a request to display all user information (listuser.do) to illustrate the use of a configuration file.





Web application based on Java EE has gradually become the main solution of e-commerce and e-government because of its hierarchical and platform-independent advantages. Aiming at the disadvantage of traditional Java EE Web application development, this paper puts forward a solution of using lightweight framework to build Web application quickly, and proves that adopting this scheme can help developers to establish clear structure, good reusability in a short time. Maintain a Web application that is easy to extend.
Reference Documents
[1] GAMMA E, HELM R, JOHNSON R, et al Design patterns:elements of reusable object-oriented software[m]. Addison Wesley, 1994.
[2] Sun Weichen. Mastering struts: MVC-based Java web design and development [M]. Beijing: Electronic Industry Press, 2004.
[3] JOHNSON R, Hoeller J, Arendsen A, et al java/j2ee application Framework reference document. V1.1.
2004.
[4] Xuchangsheng, Dai Chao. Research on a rapid development of web application methods [J]. Computer Engineering and Design, 2004, (12): 2237-2239.
[5] Xia Xin, Cao Xiaogang, Tangyong. hibernate[m]. Beijing: Electronic Industry Press, 2005.
[6] JOHNSON R.expert one-on-one ee design and development[m]. Weihaiping. Beijing: Electronic Industry Press, 2003.

When developing a Web application with ssh , you need to organize the generated classes of files, and the following is an introduction to a workable directory scenario:


For example, there is a user management module in the application, then the public package is set up under the common packet, if the public package can be Com.simon.oa,

Include the following sub-packages under the user package

1. Controler Bag

The package places the various struts action.

2. DAO Package

The package places all kinds of DAO (data Access Object), which is the implementation class that puts the access to the database, in the MyEclipse "Hibernate Reverse Engineering" A DAO that corresponds to a table is generated in a directory when it is reversed, and can be dragged into a DAO package after it is generated. In some applications, DAO is used as an interface in which all operations to the database are included, and then a hibernate package is established in the DAO package, where the implementation of the DAO interface is placed in the hibernate package. For example: The Userdao interface has an implementation class of Userdaoimpl, which puts the class into the Hibernate package , and the actual development tends to be in the latter way, because the DAO interface can implement the IOC operation of Spring. (Do not know how myeclipse to this is how to consider, this problem let me dwell for a long time, mistakenly to understand DAO as a can do the actual operation of the class, rather than an interface, later development to pay attention to)

3. Model Package

The package places hibernate reverse-engineering generated beans and the. hbm.xml file corresponding to the bean.

4. Service Package

The package places a business operation class, such as a user service class, which typically extracts an interface from the user action class and then generates a IMPL package under the service package, placing the implementation class of the user interface in the Impl package. The user interface implementation class calls the DAO interface to manipulate the database, and the method that invokes the implementation class is in the action of struts.

5. Vo Package (Value object)

The VO package includes information such as Pojo and actionform used in struts.

Vo:value Object
Dto:data Transfer Object
Personal understanding that Vo and DTOs are similar things, in principle Vo and DTOs only public fields, mainly used for data transfer between processes, VO and DTOs will not be passed to the presentation layer, will be absorbed in the business layer. But it's understandable that many people have setter,getter properties and other auxiliary methods when they build Vo and DTOs, and I'm not sure that's right.

Java Web Three-tier architecture detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.