Spring+Struts2 project framework combing
If the development is based on Spring+Struts2, the interaction logic of the front-end and back-end will be different from the organization of the bootstrap system and MCV.
Here are some notes on the learning process
Pre-existing general knowledge
Struts2 framework information
Struts2 basic chapter of the basic concepts
Java struts2 framework for learning
The general case of Spring front-end and back-end debugging process
To understand the front-end and back-end data flow in a system developed based on Spring, the following steps can be followed:
1. Front-end operations and browser debugging
-
Open the browser debugging tool: Open the web page you want to debug in your browser (e.g. the account inquiry page). Press
F12
(or right-click on the page and select "Inspect") to open your browser's developer tools. - Switch to the Network tab.: In Developer Tools, switch to the Network tab. This section will show all network requests, including AJAX requests, resource loading, etc.
- Perform query operations: Performs the "Account Inquiry" operation on the front-end page. For example, enter the query criteria and click the "Query" button.
- Observation requests: In the "Network" tab, you will see a new network request being made. This request is usually an HTTP request and may be of type GET or POST.
2. Analyzing Web Requests
- Request URL: Click on the request to see the details, including the URL of the request, the request method (GET or POST), the request header, and the request body (if it is a POST request).
- Request Parameters: If it's a GET request, the request parameters will usually be in the URL; if it's a POST request, the parameters will be in the request body. You can see how the front-end sends the data entered by the user to the back-end.
- View Response: Also in the details of this request, you can see the response data returned by the server. Usually it will be in JSON, XML, etc. format. This data is the result of the back-end processing of the request, the front-end will get it and then rendered to the page.
3. back-end debugging
-
Find the corresponding back-end controller: Based on the requested URL, you can find the corresponding controller in the Spring project (usually the controller with the
@Controller
maybe@RestController
(the annotated class). For example, if the requested URL is/api/accounts
, you can search this path in your project to find the corresponding method. - Tracking the back-end processing flow: In the controller's methods, you can see how to receive request parameters, call the service layer (Service layer) to process business logic, and finally return data.
- log output: You can debug this by adding logging to your back-end code to record how requests are received, processed, and ultimately what data is returned.
4. Combine front- and back-end data flows
- Front-end sends a request: Understand how the front-end sends user-entered data to the back-end by looking at web requests in the browser.
- The backend processes the request and returns the response: Analyze back-end code to understand how requests are processed in the back-end and how the results are returned to the front-end.
- The front-end receives and processes the response: Finally, go back to the browser and see how the front-end receives and processes the data returned from the back-end to display it in the user interface.
file
In projects where Spring is used in conjunction with Struts2, thefile is typically used to configure Action mappings in the Struts framework.
role of
-
Action mapping configuration:
The file defines the Struts Action mapping , will be a specific URL path mapped to an Action class on a method . Action class is the core component of the Struts framework , it is responsible for handling user requests , call the business logic and return the corresponding view .
-
Path to Action Mapping: When the request is initiated on the front end, Struts makes the request according to the URL path in the
to find the corresponding Action mapping, and then call the corresponding method in the configured Action class.
Find the specific path or flow of the front-end call to the back-end
-
Browser View Request URL: First, look at the request URL sent by the front-end page through your browser's debugging tools (e.g., the Network tab).This URL is usually associated with the
The paths configured in the
-
Locating Action Mappings: Based on the request URL in the browser, open the
file, find the Action mapping entry that matches that URL. This entry will tell you the corresponding Action class and method.
-
Find Action Classes: Find this Action class in your project; Action classes usually inherit from the
ActionSupport
Or realizeAction
Interface. Look up the corresponding method in the class that will process the request and return the result. - Tracking business logic: In the methods of the Action class, there are usually calls to the Service layer or business logic classes to process the request. You can trace these calls to understand the details of business processing.
- Return to View or Data: After the Action class has processed the business logic, it usually returns a view name or directly returns data (e.g. in JSON format), which can be confirmed in part by the Action's return value or the configured Result type.
Summary of the front and back end call flow
- The front-end initiates the request: The user performs an action on the front-end interface (e.g., clicking a button) and the front-end code sends an HTTP request.
-
Request Mapping to Action: The Struts framework is based on
maps the request path to the corresponding Action class and method as configured in the
- The Action class handles requests: Methods in the Action class handle requests from the front end, usually calling the business logic or Service layer.
- Return results: After processing the request, the Action class returns the result, which may be a view name or a direct data response (e.g., JSON), and the front-end receives the result and displays it to the user.
What is Action?
Action
is a core concept for processing user requests, executing the appropriate business logic, and returning the results. Specifically:
Action
Role and functioning of the United Nations Office on Drugs and Crime
-
Request processing:
Action
class is responsible for handling requests from the user. Each time a user performs an action on the front-end (e.g., submitting a form, clicking a button), the resulting request is routed through the Struts framework to the correspondingAction
Class. -
Business logic implementation:
Action
Classes will call the business or service layer to perform specific business logic. This may include data validation, database operations, calls to external APIs, etc. -
The results are returned: After processing the request, the
Action
class returns a result, which is usually a view name (such as a JSP page) or data (such as a JSON-formatted response), based on which the Struts framework decides which page to display next or which data to return.
Action
workflow
- Requests arrive at the Struts framework: The user initiates a request (e.g., submits a form) in the browser.
-
map to
Action
resemble: Struts is used according to the configuration file (e.g.) in the URL mapping, routing the request to the appropriate
Action
Class. -
fulfillment
Action
logic (loanword): Struts callsAction
classexecute()
method or other specified method that performs the business logic. -
Return processing results:
Action
method returns a string indicating the view page or response data that the frame should navigate to next.
configureAction
In Struts, theAction
Class mapping is usually done in a configuration file. You can set the mapping of classes in themaybe
You will see a configuration similar to the following in the
<action path="/login"
type=""
name="loginForm"
scope="request"
validate="true"
input="/">
<forward name="success" path="/"/>
<forward name="failure" path="/"/>
</action>
-
path
: URL path, when this path is requested, call theLoginAction
Class. -
type
: SpecificAction
Class. -
forward
: Decide which view to jump to after processing the request.
Action and Routing
StrutsAction
In a way similar to routing in Spring Boot, but the two are different in the implementation and application scenarios.
-
Struts
Action
Responsible for handling requests and mapping them to URL paths via configuration files. - Routing in Spring Boot It's more like annotation-driven request handling, which simplifies the development process and is more flexible.
What is Filter?
In Struts 2, theFilter
is a component for intercepting and processing HTTP requests.Struts 2 provides the ability to intercept and process HTTP requests by means of theFilter
to handle all requests coming into the application and distribute them to the appropriateAction
。Filter
In Struts 2 plays an important role as a bridge between the pre-processing and post-processing of requests and the implementation of business logic.
Struts 2 in theFilter
summarize
- crux
Filter
:StrutsPrepareAndExecuteFilter
- In Struts 2, the
StrutsPrepareAndExecuteFilter
is the most commonly used filter. It is a Servlet filter that intercepts all HTTP requests and passes them to the Struts 2 framework for processing. -
StrutsPrepareAndExecuteFilter
The main functions include pre-processing the request, executing the correspondingAction
The process of processing the results returned, as well as the cleanup after the entire request processing flow is completed.
- In Struts 2, the
Filter
workflow
-
Request Interception:
- When an HTTP request reaches the server, the
StrutsPrepareAndExecuteFilter
will intercept this request first. It intercepts all requests related to the application, usually configured via URL patterns (e.g./*
)。
- When an HTTP request reaches the server, the
-
Preparation of requests:
-
StrutsPrepareAndExecuteFilter
It will pre-process the request, such as setting the character encoding of the request, handling file uploads, etc. In addition, it determines whichAction
class should handle the request and map the request parameters to the appropriateAction
in the properties of the class.
-
-
fulfillment
Action
:- Upon completion of pre-processing, the
StrutsPrepareAndExecuteFilter
will pass the request to the core components of the framework to find the correspondingAction
class and executes the business logic in it.
- Upon completion of pre-processing, the
-
Results processing:
-
Action
After the class has processed the business logic, it returns a result (usually the view name).StrutsPrepareAndExecuteFilter
It will decide which page or data to display next based on the returned results.
-
-
Request Post-Processing:
- At the end of the entire request processing flow, the
StrutsPrepareAndExecuteFilter
It is also responsible for performing some cleanup operations, such as freeing resources and cleaning up thread-local variables.
- At the end of the entire request processing flow, the
How to ConfigureFilter
In Struts 2, theFilter
Usually inConfiguration is performed in the Configuration
StrutsPrepareAndExecuteFilter
When you do, you can specify which requests need to be handled by the Struts 2 framework.
Sample configuration in the:
<filter>
<filter-name>struts2</filter-name>
<filter-class>.</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern> <!-- Intercept all requests -->
</filter-mapping>
customizableFilter
apart fromStrutsPrepareAndExecuteFilter
The developer can also customize theFilter
to fulfill specific request interception and processing needs. CustomizationFilter
It can be used to implement functions such as authentication, logging, and data compression.
customizableFilter
typical example:
import .*;
import ;
import ;
import ;
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Initializing Filters
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
// Implementing Filtering Logic,For example, check the request header
("Request URI: " + ());
// Continue processing requests
(request, response);
}
@Override
public void destroy() {
// Liquidation of resources
}
}
existConfigure customization in the
Filter
:
<filter>
<filter-name>customFilter</filter-name>
<filter-class></filter-class>
</filter>
<filter-mapping>
<filter-name>customFilter</filter-name>
<url-pattern>/*</url-pattern> <!-- Intercept all requests -->
</filter-mapping>
Add an action?
what sb. is thinking
Add a new action to allow existing queries to depart that action in certain circumstances
Where to add? Front-end to call the back-end action is realized through the URL, so now there are two ways to "add" an action
1. In the interceptor intercepts the/web/Xx1Action
When it is modified directly to/web/Xx2Action
, and then follow up by going through the logic of Xx2Action
2, in the front-end call Xx1Action, in the back-end to determine the specific parameters passed by the front-end, and then jump to the execution of the Xx2Action logic
Without adding a new front-end button, the first approach seems to be more flexible, as it doesn't need to break the existing Xx1Action logic, and it's easier to migrate over to the front-end once the button has been added.
program
in order toPlayer Enquiry for the test portal (/web/PlayerInfoLogAction
) Make two attempts:
1. If the currentactionName
for"PlayerInfoLogAction", then replace it with "TestInfoLogAction"
Change from ActionFilter when theactionName=PlayerInfoLogAction
When assigning a value of "TestInfoLogAction"
At this point, it's going to goactionPool
Look for this in there.
My understanding is that inplayer_web_actionMap.xml
Add a customized action event in the following section, and create a new implementation of the action and a new fade method in the corresponding location.
2, the current end of the input account for the "xixi" is, after detecting the parameter"PlayerInfoLogAction"To make a counterpart
Attempt 1: NewTestInfoLogActionand jump to the execution
Step 1: Configure actionmap file and register beans
When adding an action or interface, the first thing to think about is to make a convention (or "declaration") about it [i.e., convention over configuration].
Agree on a new action
Since we are adding a new action, we need to have a file to "route" it.
As mentioned earlier, this file is theplayer_web_actionMap.xml
existdomains\player_web_actionMap.xml
Declare a new action in.TestInfoLogAction
<Action actionName="TestInfoLogAction" className="" facadeName="TestInfoQueryFacade" concurrencyInitNum="2" concurrencyMaxNum="13"/>
The following are the main things stipulated here:
1, the name of the new action (actionName)
2, action specific implementation code location (className)
3. action corresponding to the facade method (facadeName)
Agreeing on a new corresponding interface
The new action must have a corresponding facade method, which will call the back-end interface and implementation code.
So in fact, we will also add new interfaces, which need to be managed by spring as beans.
So here, too, the convention needs to be made, inevs_service\src\main\resources\player_service_appContext.xml
The facade method (and the object it instantiates) and the back-end interface service (and the object it instantiates) are to be agreed upon here
[Reference to other interface conventions can be written]
Step 2: Creation of corresponding documents
The first step is to initialize the filter and load the action
--> Modify the actionmap configuration file in the tomcat directory
The filter will first go and call the specific action's implementation code in thedoSometings()
-->Create
The document in thedoSometings()
The facade method will be called again
-->Create
Facade methods need to inherit theBaseFacade
Some of these methods require calls to specific implementations of TestInfoQuery.
-->Configurationplayer_service_appContext.xml
The Facade method is added to the bean container to unify the management.
-->CreateTestInfoQueryService
Interface classes, and their corresponding implementation classes
Step 3: Interceptor Jump
First of all, add the judgment, the current end of the request action isPlayerInfoLogAction
When the time comes, theactionName
Replace withTestInfoLogAction
Step 4: Populate the business logic code
Again, be clear about what you want to do:TestInfoLogAction
It also queries the account data, but when the queried player name is xixi, the returned data is replaced with XxxX.
Calling relationship: doSometings()-->() [facade method] --> TestInfoLogActionService [back-end interface]