Spring mvc is the Spring MVC framework. Can you briefly introduce how to use it
It’s very simple, it’s about creating a Project&Module, importing static pages, creating package structures (controller, service, dao, domain, utils), and creating configuration files (applicationContext.xml, spring mvc.ml, jdbc. properties, log4j. properties)
The configuration file is basically like this:
ApplicationContext.xml: This is the core configuration file of the Spring framework, used to define the beans of an application and their dependencies, AOP, transaction management, etc. It is responsible for the configuration and initialization of the entire application, including the configuration information of the Spring container.
Spring-mvc.xml: This is the configuration file for the Spring MVC framework, used to configure Web MVC related components such as processor mappers, view parsers, interceptors, etc. It will be introduced in the web.xml file through<Servlet>and<Servlet mapping>elements and integrated with the Spring container.
Dbc.properties: This is a database connection configuration file used to configure database connection information, such as database URL, username, password, etc. In Spring applications, configuration information for database connections can be obtained by reading this property file.
Web.xml: This is the deployment description file for Java web applications, used to configure and declare the properties and behavior of the application. Its role in Spring environment setup is to introduce the Spring MVC framework and specify the location of relevant configuration files.
In general, the web.xml file loads the applicationContext.xml and spring-mvc.xml Spring configuration files. When configuring the and elements in web.xml, you can specify the locations of applicationContext.xml and spring-mvc.xml to initialize and configure the Spring container and Spring MVC during application startup.
Web.xml is basically the same:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- Global initialization parameters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Spring listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC front controller --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> Explanation of the Principle: The <servlet> tag is used to define a Servlet component, where servlet-name specifies the name of the Servlet, here named DispatcherServlet. <servlet-class> specifies the Servlet class that handles requests. Here is org.springframework.web.servlet.DispatcherServlet, which is the core Servlet provided by the Spring MVC framework. The <init-param> tag is used to specify the initialization parameters, namely the contextConfigLocation parameter. This parameter specifies the location of the Spring MVC configuration file. Here it is set to classpath:spring-mvc.xml, which means that the configuration file is located on the classpath. If the configuration file is located in another path, you can modify it according to the actual situation. The <load-on-startup> tag specifies the startup sequence of Servlets. The smaller the value, the first it is loaded. Set here to 2, which means that this Servlet will be loaded and initialized when the container starts. The <servlet-mapping> tag is used to specify the URL mapping relationship of a Servlet. <servlet-name> specifies the name of the Servlet to be mapped. This is consistent with the previously configured <servlet-name>, which is DispatcherServlet. <url-pattern> specifies the URL matching rule. Here it is set to /, which means that all requests will be processed by the Servlet. When a request arrives, according to the matching rules of the URL, the container will hand over the request to the Servlet named DispatcherServlet for processing. Since <url-pattern>/</url-pattern> is set, any request will match this Servlet. Then, DispatcherServlet will distribute the request to the corresponding Controller class for processing based on the processor mapping relationship defined in the Spring MVC configuration file, and finally return the result to the client. In this way, the entire process from request to processing is realized.
Let’s briefly explain the configuration
web.xml file: The web.xml file is the deployment descriptor file for Java Web applications. It configures the properties and behavior of the entire application. In the web.xml file, the DispatcherServlet of the Spring MVC framework is configured using the and elements, and the location of the spring-mvc.xml configuration file is specified. This way, when a request arrives, the DispatcherServlet is invoked and handles the request based on the configured mappings. The main responsibilities of the DispatcherServlet are to receive requests, dispatch requests, invoke handlers, parse views, render views, and return the processing results to the client. It is the core component of the Spring MVC framework that implements the request-driven workflow, coordinating the work of various components in a loosely coupled manner.
spring-mvc.xml file: The spring-mvc.xml file is the configuration file for the Spring MVC framework, used to configure Web MVC-related components. When the spring-mvc.xml file is imported in the web.xml file, the handler mappers, view resolvers, interceptors, and other components defined in it are loaded into the Spring MVC framework. These components are called during the request process to handle requests, parse views, intercept requests, and perform other operations.
applicationContext.xml file: The applicationContext.xml file is the core configuration file for the Spring framework, used to define the application’s beans, their dependencies, AOP, transaction management, and more. In the web.xml file, the element is configured to specify the location of the applicationContext.xml file. When the application starts, the Spring container loads and initializes this configuration file, creates the corresponding bean objects, and manages their dependencies.
Additionally, the applicationContext.xml file can import the jdbc.properties file to configure the data source and related database connection information. In a Spring application, the Spring JDBC module can be used to operate on the database.
jdbc.properties file: The jdbc.properties file is a database connection configuration file used to configure the connection information for the database. In the applicationContext.xml file, the jdbc.properties file can be loaded and the property values defined in it can be injected into Spring beans using the configuration element context:property-placeholder. For example, the URL, username, and password of the data source can be configured in this file. This way, the Spring JDBC module can be used in the application to access the database and perform database connections and operations.
I am more familiar with springboot, so I will briefly talk about this. If necessary, you can refer to other websites.