SSH framework in JAVA

1. Spring

    Spring is a powerful framework that solves many common problems in J2EE development. Spring provides a consistent approach to managing business objects and encourages the injection of good habits of programming to interfaces rather than classes. Spring's architectural foundation is based on the Inversion of Control container using JavaBean properties. However, this is only part of the complete picture: Spring is unique in using the IoC container as a complete solution that focuses on all architectural layers. Spring provides the only data access abstraction, including a simple and efficient JDBC framework, which greatly improves efficiency and reduces possible errors. Spring's data access architecture also integrates with Hibernate and other O/R mapping solutions. Spring also provides the only transaction management abstraction that provides a consistent programming model across various underlying transaction management technologies such as JTA or JDBC transactions. Spring provides an AOP framework written in the standard Java language, which provides declarative transaction management and other enterprise transactions for POJOs -- if you need them -- as well as implementing your own aspects. This framework is powerful enough to allow applications to throw away the complexities of EJBs while enjoying the key services associated with traditional EJBs. Spring also provides a powerful and flexible MVC web framework that can be integrated with the IoC container.

2. STRUCTS

    Struts is an MVC framework based on the Sun J2EE platform, mainly using Servlet and JSP technology to achieve. Because Struts can fully meet the needs of application development, it is easy to use, agile and fast, and it has attracted much attention in the past. Struts integrates Servlet, JSP, custom tags and message resources into a unified framework. When developers use it for development, they do not need to code to implement the MVC pattern, which greatly saves time. Therefore, Struts is a A very nice application framework.

3. Hibernate

    Hibernate is an open source object-relational mapping framework. It encapsulates JDBC with very lightweight objects, allowing Java programmers to use object programming thinking to manipulate the database at will. Hibernate can be used in any occasion using JDBC, either in Java client programs or in Servlet/JSP web applications. The most revolutionary thing is that Hibernate can replace CMP in the J2EE architecture using EJB. , to complete the task of data persistence. , Hibernate can replace CMP in the J2EE architecture of the application EJB, complete the heavy task of data persistence.

    The three frameworks of JAVA are used to make WEB applications. Struts is mainly responsible for the display of the presentation layer. Spring uses its IOC and AOP to handle the control business (responsible for the operation of the database). Hibernate is mainly responsible for the persistence of data to the database. When using jsp servlet for web page development, there is a web. xml mapping file, there is a mapping tag in it, which is used for file mapping. When you enter the URL address on the browser, the file will correspond to a java file according to the name you wrote, and the content written in the java file will be displayed on the browser, which is a web page, so the name of the web page is written casually. , whatever name you choose, no matter what you choose from .php.jsp .do or anything else, it corresponds to this java file. The code in this java file does whatever it wants to do. It shows a sentence or connects to the database or jumps to other Pages, etc., this java file encapsulates the data, which plays a role in security and easy management. In fact, this java file is compiled as a bytecode file of .class, and there is no web page file similar to html embedded tags and codes. The difference between him and the jsp file is that the jsp embeds the code in the html tag. There is also a servlet or struts in the html form action in the submission name corresponds to a java file, struts is generally .do, are the same as the above mapping.

 Fourth, SSH framework detailed description

    First of all, SSH is not a framework, but an integration of multiple frameworks (struts+spring+hibernate). It is a popular open-source integration framework for web applications. It is used to build flexible and easily extensible multi-layer web applications.

    The system integrating the SSH framework is divided into four layers in terms of responsibilities: presentation layer, business logic layer, data persistence layer and domain module layer (entity layer).

    As the overall infrastructure of the system, Struts is responsible for the separation of MVC. In the model part of the Struts framework, it controls the business jump, and uses the Hibernate framework to provide support for the persistence layer. On the one hand, as a lightweight IoC container, Spring is responsible for finding, locating, creating and managing objects and dependencies between objects, and on the other hand, it can make Struts and Hibernate work better.

    The basic business process for building a system from SSH is: 

1. In the presentation layer, the interactive interface is first realized through the JSP page, which is responsible for transmitting the request (Request) and receiving the response (Response), and then Struts delegates the Request received by ActionServlet to the corresponding configuration file (struts-config.xml). Action processing.

2. In the business layer, the Spring IoC container that manages the service component is responsible for providing the business model (Model) component and the component's collaborative object data processing (DAO) component to the Action to complete the business logic, as well as providing transaction processing, buffer pools and other container components To improve system performance and ensure data integrity.

3. In the persistence layer, it relies on Hibernate's object mapping and database interaction to process the data requested by the DAO component and return the processing results.

    Using the above development model not only realizes the complete separation of the view, the controller and the model, but also realizes the separation of the business logic layer and the persistence layer. In this way, no matter how the front end changes, the model layer only needs few changes, and changes in the database will not affect the front end, which greatly improves the reusability of the system. Moreover, due to the small coupling between different layers, it is conducive to the parallel work of team members, which greatly improves the development efficiency.

    Let's take a closer look at the three frameworks that make up SSH:

1. Spring

1. What is Spring?

Simply put, Spring is a lightweight Inversion of Control (IoC) and Aspect Oriented (AOP) container framework.

2. Features of Spring

All of these features of Spring enable you to write cleaner, more manageable, and easier-to-test code

3. Why use Spring?

The above features of Spring enable developers to use basic JavaBeans to accomplish things that were previously only possible with EJBs. However, Spring's uses are not limited to server-side development. Any Java application can benefit from Spring in terms of simplicity, testability, and loose coupling.

2. Struts

1. What is Struts?

It implements  the application framework of MVC design pattern based on Java EE Web application by using Java Servlet/JSP technology, and is a classic product in MVC classic design pattern.

2. The core composition of Struts1

In Struts1, a servlet named ActionServlet acts as the controller (Controller), according to the struts-config.xml configuration file that describes the corresponding relationship between model, view and controller, forwards the request of the view (View), and assembles the response. Data model (Model).

In the Model part of MVC, it is often divided into two main subsystems (the internal data state of the system and the logical action to change the data state). These two conceptual subsystems correspond to ActionForm and Action in Struts 1 respectively. Need to inherit from the superclass. Here, Struts 1 can be combined with a variety of standard data access technologies, including Enterprise Java Beans (EJB), JDBC and JNDI.

On the View side of Struts 1, in addition to using standard JavaServer Pages (JSP), it also provides a large number of tag libraries, and can also be integrated with other presentation layer component technologies (products), such as Velocity Templates, XSLT Wait.

By applying the Struts framework, end users can focus most of their attention on their own business logic (Action) and the mapping relationship configuration file (struts-config.xml).

3. The basic execution process of Struts1

For details, please refer to "STRUTS Basic Workflow"

4. What is struts2

Struts 2 is the next-generation product of Struts, which is a brand-new Struts 2 framework merged on the technical basis of struts 1 and WebWork. Its brand-new Struts 2 architecture is very different from that of Struts 1. Struts 2 takes WebWork as the core and uses the interceptor mechanism to process user requests. This design also enables the business logic controller to be completely separated from the Servlet API, so Struts 2 can be understood as an updated product of WebWork

5. Architecture of strut2

Parse:

When the web container receives a request (HttpServletRequest) it passes the request to a standard filter chain including process (ActionContextCleanUp) filters, and then passes through Other filters (SiteMesh , etc),

Next, the FilterDispatcher core controller needs to be called, and then it calls the ActionMapper to determine which Action is requested, and the ActionMapper returns an ActionMaping object that collects detailed information about the Action.

Next, FilterDispatcher delegates control to ActionProxy, ActionProxy calls the Configuration Manager (ConfigurationManager) to read the configuration information (struts.xml) from the configuration file, and then creates an ActionInvocation object. ActionInvocation will call the configuration interceptor used in turn before calling Action. (Interceptor N) Once the execution result returns the result string ActionInvocation is responsible for finding the (Result) corresponding to the result string and then executing this Result Result will call some templates (JSP)

To render the page, then the interceptor (Interceptor N) will be executed (the order is reversed before the Action is executed) and the final response (HttpServletResponse) is returned to those filters and (core controller) (FilterDispatcher) configured in web.xml.

6. Why use Struts?

First, Struts is an implementation of MVC that uses Servlet and JSP tags (belonging to the J2EE specification) as part of the implementation. Struts inherits the characteristics of MVC, and makes corresponding changes and extensions according to the characteristics of J2EE, which weakens the coupling between the business logic interface and the data interface, and makes the view layer more changeable.

In addition, struts has the function of page navigation, which makes the context of the system clearer. Through a configuration file, you can grasp the relationship between the various parts of the entire system, which is of great benefit for later maintenance. Especially when another group of developers took over the project, this advantage became more obvious.

7. The difference between Struts1 and Struts2

See " Struts1 and Struts2 "

3. Hibernate

1. What is Hibernate?

Hibernate is an open source object-relational mapping framework. It encapsulates JDBC with very lightweight objects, allowing Java programmers to use object programming thinking to manipulate the database at will. Hibernate can be used in any occasion using JDBC, either in Java client programs or in Servlet/JSP web applications. The most revolutionary thing is that Hibernate can replace CMP in the J2EE architecture using EJB. , complete the task of data persistence

2, Hibernate core composition

3. Hibernate basic execution process

Why use Hibernate?

1. Encapsulates the code for JDBC access to the database, which greatly simplifies the tedious and repetitive code of the data access layer.

2. Hibernate is an excellent ORM implementation. It greatly simplifies the coding work of the DAO layer, frees software developers from a lot of the same data persistence layer-related programming work, and makes development more object-oriented.

3. It has good portability and supports various databases. If you change the database, you only need to change the configuration in the configuration file, without changing the hibernate code.

4. Support transparent persistence, because hibernate operates a pure (pojo) java class, does not implement any interface, and is not intrusive. So it is a lightweight framework.

Finally, attach the examples built by the SSH1 and SSH2 frameworks, click to download

The SSH1 framework construction instance (spring2+struts1+hibernate3) adopts the old version, see the comments in the code for details

SSH2 framework construction examples (spring3.2+strust2.3.4+hibernate4.2) all use the latest version, see the comments in the code for details

Development environment construction, refer to another blog

"Win7 (64) + eclipse (64) + tomcat7 (64) + jdk7 (64) development environment configuration"

Attached is a URL for Eclipse to build a SSH (Struts2+Spring3+Hibernate3) framework project tutorial:

http://blog.csdn.net/yeohcooller/article/details/9316923

Some notes:

  1. Code download address: http://pan.baidu.com/s/1eQh7FXo  
  2. Development environment configuration: http://blog.csdn.net/shan9liang/article/details/8807784  
  3. In the war, find application-common.xml. Modify the data source and specify your own database. After the project is deployed, the tables are automatically mapped. 
<!--配数据源 -->  

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  

destroy-method="close">  

<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  

<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />  

<property name="username" value="ssh" />  

<property name="password" value="ssh" />  

</bean>     
  1. The above is the configuration of oracle.   
  2. If you use other databases, please add the driver package in the lib directory. and modify the data source.   
  3. Strong recommendation: https://code.csdn.net/shan9liang/tgb This is hosted on CSDN CODE, an attendance management system built with ssh, maven project, more standardized than the original one, it is recommended to use this 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325301677&siteId=291194637