Using Struts+spring+hibernate to assemble Web applications

Source: Internet
Author: User

This article will discuss how to combine several well-known frameworks for loose-coupling purposes, how to build your architecture, and how to keep your application tiers consistent. The challenge is to combine these frameworks so that each layer communicates with each other in a loosely coupled way, regardless of the underlying technology. This article will use 3 popular open source frameworks to discuss the strategy of the composite framework

In fact, even if the use of Java to build a Web application is not very cumbersome, it is not an easy thing. There are many things to consider when building a framework for an application. From a high-level perspective, developers need to consider: How to build User interface? Where does the business logic work? And how to persist the application data. Each of these three layers has their own questions to answer. What technologies should be used at all levels? How can the application be designed loosely coupled and flexible to change? Does the architecture allow layer substitution not affect other layers? How does an application handle container-level services, such as transaction processing?

When creating a framework for your Web application, there are a number of issues that need to be involved. Fortunately, many developers have already encountered such recurring problems and have established a framework to deal with such problems. A good framework has the following points: ease the burden of developers dealing with complex problems ("Do not reinvent the wheel"); internally defined as extensible; There is a strong user base support. The framework is usually a good solution to the problem on one hand. However, there are several layers of your application that may require their respective frameworks. Just as you solve your user interface (UI) problem, you should not mix transaction logic and persistence logic in. For example, you should not write JDBC code inside the controller so that it contains business logic, which is not a function that the controller should provide. It should be lightweight, and proxy calls from outside the user interface (UI) to other application tiers that serve those requests. A good framework naturally forms the guidance of how code is distributed. More importantly, the framework eases the pain of developers writing code like the persistence layer from scratch, and makes them focus on the application logic that is important to the customer.

This article will discuss how to combine several well-known frameworks for loose-coupling purposes, how to build your architecture, and how to keep your application tiers consistent. The challenge is to combine these frameworks so that each layer communicates with each other in a loosely coupled way, regardless of the underlying technology. This article will use 3 popular open source frameworks to discuss the strategy of the composite framework. The presentation layer we will use struts; the business layer we will use spring; the persistence layer uses hibrenate. You can also replace any of these frameworks in your application to get the same effect. Figure 1 shows what the top looks like when these frames are grouped together.

Figure 1 Overview of building with struts, Spring, and hibernate frameworks

Hierarchy of applications

Most of the less complex Web applications can be divided into at least 4 accountability levels. These levels are: presentation layer, persistence layer, business layer, domain model layer. Each layer has a clear responsibility in the application and should not confuse functionality with other layers. Each application layer should be independent of each other but put a communication interface between them. Let's start with a look at each layer and discuss what these layers should provide and what they should not provide.

Performance Layer

At one end of a typical Web application is the presentation layer. Many Java developers also understand what struts offers. However, it is too common for them to put coupled code such as business logic into a org.apache.struts.Action. So let's agree on what a framework like struts should provide. Struts is responsible for this:

Manage requests and responses for users;

Provides a controller agent to invoke the business logic and other upper processing;

Handle an exception that throws a struts action from another layer;

Provide a model for display;

Perform user interface validation.

Here are some projects that are often written in struts but should not be associated with the Struts presentation layer:

Direct and database communication, such as JDBC call;

Business logic and validation related to your application;

Transaction management;

Introducing this code into the presentation layer will result in typical coupling and annoying maintenance.

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.