Data download link
introduce
A concise version of the management system based on the SSH framework ;
Realize login , registration , addition , deletion , modification and query ;
Can continue to improve and add front-end, verification, other functions, etc.;
Can be used as the basic model for SSH (Structs Spring Hibernate) project development exercises;
Curriculum design , graduation design development basis;
Any complex framework is a combination and extension of simple knowledge, and learning the basics is the most important thing;
This project is simple, easy to understand the basic principles, and lays the foundation for the development of complex SSM complex projects.
Environmental preparation
Development platform: Idea 2019
Database: MySQL 5.0.22
Server: Tomcat 9.0.37
Note: IDEA development platform, MySQL database, and Tomcat server need to be installed. The versions may not be exactly the same. Just do a good job in version adaptive configuration.
project structure
1. Database and Java code files
2. Jsp and other front-end code
3.Tomcat server configuration and operation
4. MySQL database
Fundamental
1. Related technologies
Structs
1. Principle of Structs1
1. Structs1 Schematic
2. Structs1 principle steps
The user enters data at the view layer.
The first step: the general controller of the Structs framework, initializes, and starts to read the strusts-config.xml file. ActionServlet, the main controller of the Struts framework, is a Servlet configured in web. The module initializes the corresponding object. (object-oriented thinking)
Step 2: The view layer sends an Http request to the general controller, the user submits a form or submits a request to the WEB server through the URL, and the requested data is transmitted to the web server using the HTTP protocol
The third step: Form filling; ActionServlet, the general controller of structs, puts the data into the member variable in the corresponding form object when the user submits the request.
Step 4: Dispatch the request; the controller dispatches the request to the specific Action according to the configuration information, and the object ActionConfig, and the corresponding formBean is passed to the execute() method in the Action.
Step 5: Processing business; Action generally only includes an execute() method, which is responsible for executing the corresponding business logic (calling other business modules) and returns an ActionForward object after completion. The server forwards the work through the ActionForward object.
Step 6: Return a response; Action returns a target response object to the general controller with different results of business processing.
Step 7: Find the response; the general controller finds the corresponding resource object according to the target response object returned by the Action processing business, which is generally a jsp page.
Step 8: Respond to the user; the target response object passes the result to the resource object, and presents the result to the user.
3. Advantages and disadvantages of Structs
advantage:
① Open source framework with clear structure ② Classic implementation of MVC (MVC is an idea, not a technology) ③ Handling exception mechanism to achieve internationalization ④ Powerful tag library , easy to maintain ⑥ will be automatically submitted in the formBean, will not use the traditional get, set method to get the value, take the value
shortcoming:
① Complex configuration ② Inconvenient testing ③ Rely on web container ④ Action is a singleton mode and must be set as thread-safe
Two, Structs2 principle
1. Structs2 Schematic
2. Struct2 principle steps
Step 1: The client initiates a request to a Servlet container (such as Tomcat)
Step 2: The request goes through a series of filters (Filter)
Step 3: Then the FilterDispatcher is called, and the FilterDispatcher asks the ActionMapper to decide whether it needs to call an Action
Step 4: If ActionMapper decides to call an Action, FilterDispatcher hands over the processing of the request to ActionProxy
Step 5: ActionProxy asks the configuration file of the framework through the Configuration Manager to find the Action class that needs to be called
Step 6: ActionProxy creates an instance of ActionInvocation
Step 7: The ActionInvocation instance is called using the naming pattern, which involves the call of the relevant interceptor (Intercepter) before and after the process of calling the Action
Step 8: Once the Action is executed, ActionInvocation is responsible for finding the corresponding return result according to the configuration in struts.xml. The returned result is usually (but not always, it may be another Action chain) a JSP or FreeMarker template that needs to be represented. Tags inherited from the Struts2 framework can be used in the representation process. ActionMapper needs to be involved in this process
Note: All objects (Action, Results, Interceptors, etc.) in the above process are created through ObjectFactory
3. Advantages and disadvantages of Structs2
advantage:
①A large number of interceptors: Struts2 itself provides a large number of reusable interceptors, such as type conversion interceptors, which often obtain parameters from the page. At this time, it is of String type and needs to be manually operated.
② Plug-in-based framework: Struts2 is a plug-in-based framework. The community provides many useful plug-ins, such as jfreechat/json, etc. Using these plug-ins can simplify our development and speed up the development progress. The biggest shortcoming of Struts2 is that it does not support well on many web servers. For example, websphere5.5, weblogic8.1 and previous versions support very difficult checks, and the latest ones need to be used.
③Support for multiple views: Support for multiple views: jsp, freemarker, Veloctiy, and even support pdf through easy transformation, and multiple views can be supported in the same project.
④ More modularization: Compared with Struts1.X, Struts2 is more modular, and can easily split configuration information into multiple files according to functional boundaries, which is convenient for management and team collaboration development.
⑤ Integration with Spring: Compared with Struts1.x, Struts2 does not need to write a singleton by itself, which further reduces the coupling between programs. As far as Struts2 itself is concerned, it reduces the coupling of the framework itself.
⑥Easy testing based on pojo: In Struts1.x, these two Http objects need to be Mocked, and it is difficult to write Action unit tests. It is more convenient to write unit tests for Action.
shortcoming:
①It is troublesome to obtain the parameters passed from jsp in Action in Struts2. Getter and Setter methods can be configured for the properties in the Action of Struts2, and the request parameters can be set to these properties through the default interceptor. But when there are many request parameters, the Action class becomes bloated. The attributes in Action can not only be used to obtain request parameters but also can be output to Jsp, which will be more messy.
② Verification is more cumbersome. If the verification is wrong, only some information can be prompted to the user. If there are multiple fields, and each field returns to a different screen when an error occurs, it is not easy to implement under the Strut2 framework with the help of the verification logic provided by the framework.
③Security needs to be improved. A remote code execution vulnerability when using the abbreviated navigation parameter prefix and an open redirection vulnerability when using the abbreviated redirect parameter prefix.
Spring
1 Introduction
Spring is an open source framework, a lightweight Java development framework that emerged in 2003, and was created to solve the complexity of enterprise application development. Simply put, Spring is a lightweight Inversion of Control (IOC) and Aspect-Oriented (AOP) container framework. In daily development, IOC containers are widely used, and Beans can be loaded, that is, classes in Java, and do not need to be initialized every time a class is used, and the keyword new rarely appears. In addition, spring's AOP, transaction management, etc. will also be used frequently.
IOC: Inversion of Control is a design idea to reduce the coupling relationship between objects. For example: renting a house, usually renting a house requires you to look for houses one by one by yourself, and then after using the IOC, you need to find a rental agency, tell the housing demand to the intermediary, and you can directly find a suitable house. That is, the requirements are handed over to a third party, and the third party helps you fulfill your requirements. The intermediary is equivalent to the Spring container.
AOP: Aspect-oriented programming is a supplement to object-oriented development, allowing developers to dynamically modify the model to meet new requirements without changing the original model, such as: dynamically adding logs, security or exception handling, etc. AOP reduces the coupling degree between various parts of business logic, improves program reusability, and improves development efficiency.
2. The main features of Spring
①Lightweight: Spring is a lightweight development framework, the basic version is only about 2M
②IOC: Inversion of Control, the dependencies between objects in the application are controlled by Spring
③AOP: Aspect-oriented programming, Spring supports aspect-oriented programming, so that business logic and system services can be separated and separated
④Container: Contains and manages the life cycle and configuration information of objects in the application
⑤ MVC idea: Spring is also a framework based on MVC idea, and also follows the three levels of model-view-controller
⑥Transaction: spring provides a continuous interface, which can be extended from local affairs to global affairs
⑦Exception handling: Spring provides a convenient API to convert specific technology-related exceptions (such as those thrown by JDBC, hibernate, or JDO) into consistent unchecked exceptions.
3. Spring advantages and disadvantages
advantage:
①Spring can effectively organize your middle-level objects.
②Spring can eliminate the excessive use of Singleton that is common in many projects. It reduces the testability and object-oriented degree of the system.
③By handling configuration files in a consistent way across different applications and projects, Spring can eliminate the need for various custom-formatted property files.
④By reducing the cost of programming to interfaces rather than to classes to almost nothing, Spring can promote good programming habits. ⑤Spring is designed to make applications created using it rely as little as possible on his APIs. Most business objects in a Spring application do not depend on Spring.
⑥ Applications built with Spring are easy to unit test.
⑦Spring can make the use of EJB an implementation choice, rather than an inevitable choice of application architecture. You can choose to use POJOs or local EJBs to implement business interfaces without affecting the calling code.
⑧Spring helps you solve many problems without using EJB. Spring can provide an alternative to EJB, which is suitable for many web applications. For example, Spring can use AOP to provide declarative transaction management without going through an EJB container, and you don't even need a JTA implementation if you only need to deal with a single database.
⑨Spring provides a consistent framework for data access, whether JDBC or O/R mapping products (such as Hibernate) are used.
shortcoming:
①There is a lot of code to be written in jsp, the controller is too flexible, and there is a lack of a common controller
②Spring does not support distributed, which is one of the reasons why EJB is still in use.
Hibernate
1. The working principle of hibernate
The first step: Configuration.config() to read the xml configuration file
Step 2: Configuration.config() to read the mapping information in the configuration file
Step 3: Create a session factory
Step 4: Open the session
Step 5: Start the transaction
Step 6: Persist to the database
Step Seven: Close the session
Step 8: Close the session factory
2. The advantages and disadvantages of hibernate
advantage:
①hibernate is an open source framework based on ORMapping technology, which encapsulates JDBC in a lightweight way, and uses object-oriented thinking to manipulate the database.
②hibernate provides session cache and secondary cache, which improves performance for systems that do not require complex queries.
③Low intrusive design
shortcoming:
① hibernate is not easy to learn, and the learning cost is too high
② Hibernate is not suitable for complex queries (statistics) because it does not directly operate on the underlying database
③Not suitable for a large number of aggregation operations (stored procedures)
2. Fundamentals
SSH is an integrated framework of Struts, Spring, and Hibernate. It is currently a popular open source framework for web applications, used to build flexible and easily scalable multi-tier web applications.
The basic business process of SSH build system:
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 business jumps 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 system of 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).
The project of the SSH framework, which is mainly divided into three levels:
(1) Struts2: responsible for the web layer
(2) Spring: Management of the business layer
(3) Hibernate: responsible for data persistence
In the presentation layer, first implement the interactive interface through the JSP page, responsible for transmitting the request (Request) and receiving the response (Response), and then Struts delegates the Request received by ActionServlet to the corresponding Action according to the configuration file (struts-config.xml) .
In the business layer, the Spring IoC container that manages the service components is responsible for providing the Action with the business model (Model) component and the component's collaborative object data processing (DAO) component to complete the business logic, and provides container components such as transaction processing and buffer pool to improve system performance and ensure data integrity.
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 result.
Adopting the above development model not only realizes the complete separation of view, controller and model, but also realizes the separation of business logic layer and persistence layer. No matter how the front end changes, the model layer only needs a few changes, and the change of 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 beneficial for team members to work in parallel, which greatly improves the development efficiency.
page display
1. Login page
2. Registration page
3. Homepage
4. Add new page
5. Delete page
6. Modify the page