Self-understood J2EE three-tier architecture (different from the software design model)

Source: Internet
Author: User

For example

1. J2EE is divided into three layers:
Server-side business logic (including business logic layer and persistent data layer, businness tier and EIS tier), server-side presentation layer (Web Tier), and client presentation layer (Client Tier)

The J2EE design pattern can be summarized into six categories.
(1) presentation layer architecture (server-side presentation layer)
A. Front-end controller mode
B. MVC Mode
C. decorator Mode
 
(2) Presentation Layer high-level Architecture
A. composite view mode (presentation layer on the server side)
B. View assistant Mode
C. Service Worker Mode
 
(3) Presentation Layer Scalability (server-side presentation layer)
A. asynchronous page Mode
B. cache filter mode
C. Resource Pool Mode
 
(4) business layer mode (server-side business logic layer)
A. composite entity Mode
B. domain object mode (business data layer)
 
(5) data transmission mode (used between the business logic layer and the presentation layer)
A. DTO Mode
B. Pass hash mode with DTO
C. Database row set DTO Mode
 
(6) database mode (server persistence data layer)
A. DAO mode
B. Dao factory Model

2. Front-end controller mode
A common application is that a servlet acts as a front-end controller, which forwards requests to the page controller based on the page request. The page controller is also a servlet. this servlet electrophoresis the business logic that the request should execute and returns the actual page according to the results of the business logic. The simplified usage is that the front-end controller servlet uses the "command mode" to convert the page controller into more fine-grained classes.

Acitonservlet is a front-end controller, part of the code

Requestutils. selecetmodule (request, getservletcontext ());
Getrequestprocessor (getmoduleconfig (request). Process (request, response );

The process method of requestprocessor processes public tasks, some code
If (! Processpreprocess (request, response )){
Return;
}
All processxxx methods are processing public actions.

The processactionperform method of requestprocessor implements the command mode, which assigns the specific request action to the action.
Some code
Return(action.exe cute (mapping, form, request, response ));

The front-end controller selects the page controller to parse users' requests, implement specific business logic, and forward the results to the page

3. decorator Mode
(1) decorator mode in Design Mode
The decorator mode provides a method for the client to transparently expand the functions of an instance. The return type of this method is the instance. When the client continuously calls this method, the internal representation of the instance has changed completely with the function change.
(2) decorator mode in J2EE design mode
Servlet supports the decorator mode to describe a request, and use the decorator class to intercept all requests sent to the target object and perform the required work, after the completion, the request will be forwarded to the next decorator, knowing that there is no decorator, and finally sending the request to the Servlet

Servlet filters are used to intercept requests and responses. Before a request arrives at the servlet, additional processing is performed for this request. The processor can be regarded as a chain, and each filter can be transmitted to each other. Below are three parameters of the filter interface dofilter: servletrrequest, servletresponse, filterchain, and dofilter () of filterchain () activate the next related Filter

Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {
// Use the secletencoding method to set the character encoding
If (ingnore | request. getcharacterencoding () = NULL ){
String encoding = selectencoding (request );
If (encoding! = NULL)
Request. setcharacterencoding (encoding );
}
Chain. dofilter (request, response );
}

4. composite view mode
"Merging mode" in Design Mode"
Merging mode: provides a tree-like object structure. Both the tree branches and the leaf classes implement the same interface, so that the client can call the Root Interface of the tree structure when calling any object.

"Composite view mode" in J2EE design mode"
Extract the view layout from it to form a template composed of a series of common components. You can use XML to describe the composition of a view.

5. View assistant Mode
The tag library of the JSP page and the struts tag library. A tag should inherit from javax. servlet. jsp. tagext. tagsupport and provide the Implementation of dostartag and doendtag methods.
Dostartag implements business logic
Doendtag control output

6. Service Worker Mode
The page flow, front-end controller mode, and view assistant mode are combined to represent a complete set of processes for "request-forwarding-View. This mode is also the implementation standard of the MVC mode, and Struts is also implemented based on this mode

7. asynchronous page Mode
When remote data changes, it is cached as the "publisher-subscriber-model ". The function of J2EE is to use a subscriber role to accept data from the publisher role at a time interval or when data changes. The Subscriber role also updates the database using the mode. This kind of work goes through the "Observer" pattern in software design patterns ". A common application is to use a subscriber role to display the HTML page with the latest information on the Publishing Server. For example, actionservlet

8. cache filter mode
This mode is used to dynamically generate pages in the village to minimize the number of duplicate pages. The function of J2EE is to use a cache filter to intercept a request and determine whether the page returned by the request has a cache. The cache filter should be placed before the "decoration filter" and work servlet. The cache filter is a variant of the decoration filter. Decorate the httpservletresponse object to save the request processing result.

9. Resource Pool Mode
When the client needs a JDBC connection, it should be obtained from a pool. If a JDBC connection is available in the pool, this object resource is returned. If there are no available resources but the pool has capacity, use the factory to generate a new instance. If the pool does not have any available resources and the pool capacity is full, you must wait and know that other clients return at least one object.

10. composite entity Mode
This mode can reduce the complexity and open credit in the work environment. A composite entity aggregates data from different sources into a single object. The application is a centralized object in the EJB environment.

11. domain object mode
Converts a table structure in a database into an object, for example, a table in hibernate, which is used in the persistence layer framework theory.

12. DTO Mode
The actionform of Struts is implemented in the DTO mode. The data obtained from the page view is transmitted to the model layer. After the model layer calls the business logic, it returns the data to the actionform for data display at the view layer.
Actionform is only used to pass data from the view layer to the model layer.

13. DTO hash Mode
Struts's dynaactionform is a DTO hash mode that allows programmers to set a primary key for a data, and then the page data can be obtained through this primary key in Acton.

14. DTO mode of database row set
Send the JDBC resultset to the client for display, and use an independent class of the resultset interface as the DTO to send to the client for display.

15. DAO mode
The Data Access Object Mode is considered as a basic mode of the persistent layer.
Dao has the function of querying data and processing business logic, and is no longer popular.

16. Dao factory Model
Users are only interested in the products they have created, and many additional work they have done before creating these products is encapsulated into the subclass of the factory interface, it does not apply to constructors of specific product classes to achieve implicit usage.

The change mode only supports database connection and does not support data access.

17. Differences between the J2EE design mode and the design mode
(1) The software design mode is design, and the J2EE design mode focuses on architecture.
(2) The software design model aims to extract the best time to solve common problems in the various software world. It is a summary of the continuous success and failure of many experienced software designers.
To solve the architecture problems of enterprise-level applications.
(3) The main purpose of the software design pattern is decoupling. The software design pattern can appear at any layer of the J2EE model.
The J2EE design mode decouples the layers in the J2EE model. The Software Design Pattern focuses on the micro methodology, while the J2EE design pattern focuses on the macro methodology.

18.
(1) the front-end controller mode, MVC mode, and service worker mode are integrated into the presentation layer framework, such as the Struts framework and webwork framework.
(2) The composite view mode is implemented using the tiles framework.
(3) the main implementation of the view assistant mode is the tag library and jstl tag library.
(4) With the dependency injection of the middle layer framework spring, Dao factory mode does not need to be implemented.

 

The following is an article from netizens:

I. MVC Architecture
Struts is a good MVC Architecture. I have always used it. Through simple configuration, we can combine view, controler, and model. View is mainly implemented by JSP, because it is tag-oriented, so it provides a good interface for web designers. Formbean is an intermediate data carrier between JSP and action. It undertakes the data transfer process from JSP to action. Action is a transfer station for processes. Different businesses are called with different models in different actions. Model is the operation process to implement specific services. However, this process is implemented at a high level.
In short, the MVC Architecture implements two layers in the three-layer structure, namely the presentation layer and the business layer, and the other layer is called the persistence layer.
Layer 2 Architecture
As mentioned above, the three-tier architecture is the "presentation layer", "business layer", and "persistence layer ". The presentation layer Implementation works on behalf of the Struts framework, the business layer Implementation works on behalf of spring, and the persistence layer Implementation works on behalf of hibernate. However, in my development, spring has been removed because I think the business is too simple to be implemented using spring. Next I will introduce my three-layer implementation in detail.
1. Three types of beans
In my implementation, there are a total of three beans that exist to save the object state. In the persistence layer, this bean is a pojo object. It is database-oriented, or has to take care of the implementation of the database, because I am used to using powerdesigner to design the database, and the structure made is quite satisfactory, however, if Hibernate is used directly to implement the database, it is not that clean. Therefore, the design of pojo objects cannot be fully object-oriented. The bean in the business layer is called entity, which is a bean completely oriented to program logic. It may be a collection of several pojo. For the business logic, this bean called entity can be used for ease of business. The bean in the display layer is formbean, which is mainly used for data input.
Pojo only exists in the persistence layer, and data is handed over to entity at the business layer. The entity not only exists in the business layer, but is also transmitted to JSP for display.
2. pojo and Dao
I think this is the relationship between data and operations. Dao only performs the call operation, while pojo only saves the data. The following is an example of my dao interface.
Public interface Dao {

/
Public void resetpo (pojo ){
This. userpo = (userpo) pojo;
}


Public String save (){
String OID = NULL;
Try {
// Session = hibernateutil. currentsession () has been initiated to the constructor for initialization.
Transaction Tx = session. begintransaction ();
If (userpo. GETID () = NULL ){
Oid = (string) Session. Save (userpo); // if this is a new pojo, perform the insert operation.
Session. Flush ();
TX. Commit ();
} Else {
Session. Update (userpo, userpo. GETID (); // update the pojo if it has been initialized
Session. Flush ();
TX. Commit ();
}
} Catch (hibernateexception ex ){
System. Out. println ("// n userdao. Save () exception! ");
Log. Error ("userdao. Save () exception! ", Ex );
}
Return OID;
}

 
Public pojo load (){
Userpo TMP = new userpo ();
Try {
// Session = hibernateutil. currentsession () has been initiated to the constructor for initialization.
TMP = (userpo) Session. Get (userpo. Class, userpo. GETID (); // obtain the object with the exact id value
} Catch (hibernateexception HBE ){
HBE. printstacktrace ();
}
If (TMP! = NULL ){
Userpo = TMP;
Return userpo;
}
Else
Return NULL;
}

 
Public list find (short ){
// Session = hibernateutil. currentsession () has been initiated to the constructor for initialization.
Query query = NULL;
String where = NULL;
Switch (){
Case 0:
Where = "";
Break;
Case 1:
Where = "where us. Name = '" + userpo. getname () + "'";
Break;
Case 2:
Where = "where us. Account = '" + userpo. getaccount () + "'";
Break;
Case 3:
Where = "where us. Password = '" + userpo. GetPassword () + "'";
Break;
Case 4:
Where = "where us. Account = '" + userpo. getaccount () + "' and us. Password = '" + userpo. GetPassword () + "'";
}
Query = session. createquery ("from userpo us" + where );
Return query. List ();
}

Public void close (){
Hibernateutil. closesession ();
}

}
Hibernateutil is a hibernate method class that provides thread-safe sessions. And the method to close this session (although it is too simple to close the session ). Each Dao is generated by daofactory. Daofactory also has a public interface. As follows:
Public interface daofactory {
 
Dao getdocumentdao ();
Dao getdocheaddao ();
Dao getdocaccessorydao ();
Dao getuserdao (User U );

}
The following is an implementation of Dao Based on hibernate.
Public class hbdaofactory implements daofactory {

Public hbdaofactory (){
}

 
Public Dao getdocumentdao (){

Return new COM. CECs. Dao. documentdao ();
}

 
Public Dao getdocheaddao (){

Throw new java. Lang. unsupportedoperationexception ("method getdocheaddao () not yet implemented .");
}

 
Public Dao getdocaccessorydao (){

Throw new java. Lang. unsupportedoperationexception ("method getdocaccessorydao () not yet implemented .");
}

 
Public Dao getuserdao (User U ){
Return new userdao (U );
}

}
In this way, you do not need hibernate to implement the persistent layer, or you can easily change it to another Dao without modifying the business layer.

 

 

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.