Java Web layer-3 Architecture

Source: Internet
Author: User

Wang fei _

Java layer-3 architecture SSH

A spring2.5 + hibernate3.2 + struts2.0 combination framework, using springOfIOC to manage applicationsOfAll beans, including struts2OfAction, giving full play to the lightweight Spring frameworkOfAdvantage.

Abstract:Aiming at the current problems faced by web application development, combined with the popular open-source frameworks spring, Struts and hibernate, a lightweight solution for developing J2EE Web applications is proposed, to help developers build Web applications with clear structures, good reusability, and convenient maintenance in a short period of time. In addition, the case shows how to apply this solution to a real project.
Keywords:J2EE MVC struts spring hibernate

The development of large-scale enterprise web application systems usually requires a good software architecture to facilitate collaborative development and expansion and upgrade. The traditional development mode cannot meet these requirements well. Aiming at the current problems faced by web application development, this paper proposes a lightweight solution for developing J2EE enterprise-level Web applications based on the popular open-source framework SSH (Spring, struts, and hibernate, use Cases to describe how to apply this solution to actual projects.
1. framework technology
The famous software Master Ralph Johnson defines the framework as follows: the Framework is a reusable design of the entire system or system, composed of a group of abstract classes and their interaction methods between instances [1].
Frameworks generally have plug-and-play reusability, mature stability, and good team collaboration. The complex multi-layer structure of J2EE determines that a large J2EE project needs to use the framework and design mode to control the software quality. Currently, some commercial and Open-Source J2EE-based application frameworks are available in the market. The mainstream frameworks include: struts framework based on MVC mode, Spring framework based on IOC mode, and object/link ing framework hibernate.
1.1 Presentation layer framework struts
Struts is an MVC Framework Based on JSP model2. It consists of model, viewer, and controller, its main design concept is to decouple the presentation logic from the business logic through the Controller to improve the maintainability, scalability and reusability of the system [2]. The architecture 1 of the Struts framework is shown in.

The architecture diagram shown in Figure 1 analyzes the MVC component in the Struts framework.
(1) view: the view is mainly composed of JSP pages. There is no process logic, business logic, and model information, and only tag. Struts itself contains a set of tag libraries (taglib), which is also one of the essence of Struts. Using them flexibly can simplify JSP page code and improve development efficiency.
(2) controller: the Controller in struts is its own actionservlet. Actionservlet receives all requests from the client and transfers control to the appropriate action object as defined in the configuration file (struts-config.xml.
(3) model: struts does not define the implementation of a specific model layer. The model layer is usually closely related to the business logic and has continuous requirements. At present, there are some excellent tools in the commercial field and the Open Source world to facilitate the development of the model layer.
1.2 business logic layer framework spring
Spring is a powerful Lightweight Framework that solves many common problems in J2EE development and can replace EJB technology. Here, lightweight refers to the Spring framework itself, rather than spring can only be used for lightweight application development. The lightness of spring is embodied in the infrastructure of its framework and its support and Assembly capabilities for other application tools. Compared with EJB, spring can reduce the risks between different technical layers.
The core of the Spring framework is to control the flip IOC (inversion of control)/dependency injection di (Dependence Injection) mechanism. IOC refers to the relationship between components controlled by containers (here, containers refer to a standardized runtime environment that provides specific services and technical support for components) rather than directly controlling program code in non-traditional implementations, this transfer of control from program code to external containers is called "flip" [3]. Di is a more vivid explanation of IOC, that is, the container dynamically injects dependencies (such as constructor parameters, constructor objects, or interfaces) into components during runtime [3]. Spring uses a set-value injection (using the setter method to implement dependencies) and a sub-injection (implementing dependencies in the constructor) mechanism to manage the set-up collaboration objects through the configuration file, create an IOC container that can construct components. In this way, you do not need to write the factory mode, Singleton mode, or other constructor methods, you can directly obtain the required business components through the container. The structure 2 of the Spring framework is shown in.

 

The Spring framework consists of seven clearly defined modules. Each module or component can exist independently or be implemented together with one or more modules. Spring core container is an IOC container used to manage business components and is the core of spring applications. Spring Dao and spring ORM not only provide abstract modules for data access, it also integrates support modules for popular Object relationship ing frameworks such as Hibernate, JDO, and ibatis, and provides important service functions such as buffer connection pool and transaction processing, this ensures the system performance and data integrity. The sprnig web module provides some abstract encapsulation of web applications and can integrate Struts, webwork, and other Web frameworks with spring into a solution suitable for you.
The Spring framework can be an all-in-one solution for enterprise-level applications. It is also a modular framework that allows developers to freely select modules suitable for their own applications for development. The Spring framework is a loosely coupled framework. The coupling of some components of the framework is designed to be the smallest. The specific framework selected at each layer depends on the needs of developers.
1.3 Data Persistence Layer framework hibernate
O/R Mapping technology is generated to solve the mismatch between relational databases and object-oriented programming. Hibernate is currently the most popular o/R Mapping Framework. It implements an automatic ing between relational databases and Java objects, allowing programmers to perform database operations in a very simple way. Hibernate Working Principle 3 is shown in.

 

Hibernate encapsulates JDBC to shield programmers from underlying database operations, so that programmers can focus on OO program development and improve development efficiency. What programmers need to do to access the database is to compile an XML ing file for persistent objects [4].
To change the underlying database, you only need to change the Initialization Configuration File (hibernate. cfg. xml or hibernate. properties) without affecting the application.
Hibernate has its own Object-Oriented Query Language, hql, Which is powerful and supports most mainstream databases, such as Oracle, DB2, MySQL, and Microsoft SQL Server, it is currently the most widely used o/R ing tool. Hibernate provides underlying support for rapid application development.
2. Design and Implementation of a Web application model based on the SSH combination framework
2.1 new J2EE framework integrated with SSH
The preceding three framework technologies based on J2EE are analyzed. The following three framework technologies are integrated to improve the traditional J2EE Web development model to form a new and Lightweight J2EE architecture.
IntegrationSSH frameworkSystemFrame chart4. The system has four levels of responsibilities: presentation layer, business logic layer, data persistence layer, and domain Module layer. Struts is used as the overall infrastructure of the system and is responsible for the separation of MVC. In the model section of the Struts framework, Hibernate framework is used to provide support for the persistent layer and spring support for the business layer. The specific method is to use the Object-Oriented Analysis Method to propose some models as needed, implement these models as basic Java objects, then compile the basic Dao interface, and provide the DAO implementation of hibernate, implemented using the hibernate Architecture
DAO class to implement conversion and access between Java classes and databases, and spring completes the business logic.

 

The basic business flow of the system is: in the presentation layer, first through the JSP page to achieve the interactive interface, responsible for sending the request and receive the response, then struts according to the configuration file (struts-config.xml) delegates The request received by the actionservlet to the corresponding action for processing. In the business layer, the Spring IoC container that manages service components is responsible for providing the business model component to the action and the collaborative Object Data Processing (DAO) component of the component to complete the business logic, it also provides container components such as transaction processing and buffer pool to improve system performance and ensure data integrity. In the persistence layer, Hibernate's object-oriented ing and database interaction are used to process the data requested by the DaO component and return the processing result.
The above development model not only achieves the complete separation of views, controllers and models, but also achieves the separation of the business logic layer and the persistent layer. In this way, no matter how the front-end changes, the model layer requires only a few changes, and database changes do not affect the front-end, greatly improving the reusability of the system. In addition, because the coupling between different layers is small, it is beneficial for team members to work in parallel and greatly improves the development efficiency.
2.2 Based onSSH frameworkWeb Application System implementation
The following uses an actual system to demonstrate how to performSSH frameworkWeb application development. This system is a Q & A system developed for the operation department of a communication company. Its functions are similar to Baidu knows and Sina love Q &. As there are many modules in the system, the following uses a user management module as an example to describe the development and implementation process of the system, the system construction process is described in the order of data persistence layer, business logic layer, and presentation layer.
(1) Data Persistence Layer
The data persistence layer consists of Java object persistence class and Data Access Object (DAO. Each database table corresponds to a persistent object, which makes it easier for developers to design and develop with OO ideas. It also shields specific databases and specific data tables and fields, eliminates the disadvantages of hard coding for database operations in terms of reusability. The structure of the user information table is shown in table 1.

Hibernate associates objects with relational data through Mapping Files. Therefore, it is necessary to write Java persistence classes and corresponding ing files corresponding to database tables. With the Java persistence class, you can implement the data persistence class on this basis. In the Spring framework, the Data Handler class can be inherited from the helper class hibernatedaosupport, which greatly facilitates the use of The Hibernate framework in spring. The corresponding code is 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 spring containers at the business layer.
(2) Business logic layer
The business logic layer is supported by the Spring framework and provides service components for processing business logic. Developers need to Model Business Objects, abstract business models, and encapsulate them in model components. Because the data persistence layer implements Java persistence classes and encapsulates data access objects (DAO), you can easily call the DaO component in the model component to access data. The IOC container of spring manages the model component and Dao component in a unified manner, as well as the transaction processing and buffer connection pool service components provided by spring.
In the user management module, a user model userservice class is created through business modeling, which encapsulates user permission management and credit management functions. The userservice class calls the Data Handler class userdao to perform operations on user data. The relationship between these components is linked by configuring applicationcontext. xml of the Spring framework. The main content of the configuration file is as follows:

   


(3) Presentation Layer
The presentation layer combines the taglib library of JSP and Struts to process the display function, and uses actionservlet to send requests (*. do) maps to the corresponding action, and the Action calls the service component of the business logic, and then jumps to the response page specified by the forword object according to the processing result.
The deployment of business processes is completed by the struts-config.xml. The following uses listuser. Do as an example to describe how to use the configuration file.





With its hierarchical and platform-independent advantages, J2EE-based Web applications have gradually become the main solutions for e-commerce and e-government. In view of the disadvantages of traditional J2EE Web application development, this paper proposes a solution that uses a Lightweight Framework to quickly build Web applications, it is proved that this solution can help developers establish web applications with clear structures, good reusability, and convenient scalability in a short time.
References
[1] gamma E, helm R, Johnson R, et al. design patterns: Elements of reusable object-oriented software [M]. Addison Wesley, 1994.
[2] sun weiqin. proficient in struts: MVC-based Java Web design and development [M]. Beijing: Electronics Industry Press, 2004.
[3] Johnson R, hoeller J, arendsen A, et al. Java/J2EE application framework reference document. v1.1.

2004.
[4] Xu Changsheng, Dai Chao. Research on a method for rapid development of Web applications [J]. Computer engineering and design, 2004, (12): 2237-2239.
[5] Xia Xin, Cao Xiaogang, and Tang Yong. A brief introduction to hibernate [M]. Beijing: Electronics Industry Press, 2005.
[6] Johnson R. Expert one-on-one J2EE design and development [M]. Translated by Wei Haiping. Beijing: Electronics Industry Press, 2003.

In useSSHWhen developing a web application, you needOfOrganize various class files. The following is a feasibleOfDirectory solution introduction:

 

For example, if an application has a user management module, a user package is created under the Public package. For example, the Public package can be com. Simon. OA,

Include the following sub-packages under the user package

1. controler package

This package contains various strutsOfAction.

2. Dao package

This package contains various types of Dao (Data Access Object), that is, placing access to the databaseOfImplementation class in myeclipseOf"Hibernate reverse engineering" will generate a corresponding table in a directory during Reverse operations.OfDao. After being generated, you can drag the DAO to the DaO package. In some applications, Dao is used as an interface, which includes allOfOperation method, and then create a hibernate package in the DaO package, put the DaO interface in the hibernate packageOf
For example, the userdao interface has an implementation class userdaoimpl, which is placed in the hibernate package.OfDevelopment tends to be the latter method, because the DaO interface can implement springOfIOC operation. (I don't know how myeclipse considers this.OfThis problem has plagued me for a long time and mistaken Dao into a practical operation.OfClass, rather than an interface, you should note later)

3. Model package

Place hibernate reverse engineering in this package to generateOfBean corresponds to this beanOf. HBM. xml file.

4. Service Package

This package is placed in the business operation class, such as the user service class. Generally, this user operation class is extracted from an interface, and an impl package is generated under the service package. Only the user operation interface is placed in the impl package.OfImplementation class. This user interface calls the DaO interface in the implementation class to operate the database, and calls this implementation classOfMethod In strutsOfAction.

5. VO package (Value Object)

VO packageOfIncluding strutsOfPojo and actionform.

VO: Value Object
Dto: data transfer object
I personally understand that vo and DTO are similar.OfIn principle, vo and DTO only have public fields, which are mainly used for data transmission between processes.OfProblem: Vo and DTO will not be passed to the presentation layer, and will be absorbed at the business layer. However, many people see that when establishing vo and DTO, they also contain the setter and getter attributes and some otherOfThis is also understandable, and I am not sure if this is correct.

# Technical Exchange

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.