1. Spring and Web environment integration
1.1 Application Context acquisition method
The application context object is obtained through the new ClasspathXmlApplicationContext (spring configuration file) method, but each time you get the Bean from the container, you must write new ClasspathXmlApplicationContext (spring configuration file). The disadvantage is that the configuration file is loaded multiple times and the application context object is created. repeatedly.
In a web project, you can use ServletContextListener to monitor the startup of the web application. When the web application starts, we can load the Spring configuration file, create the application context object ApplicationContext, and store it in the largest domain servletContext domain, so that you can Obtain the application context ApplicationContext object from the domain at any location.
1.2 Spring provides tools for obtaining application context
The above analysis does not need to be implemented manually. Spring provides a listener ContextLoaderListener that encapsulates the above functions. The listener loads Spring configuration files, creates application context objects, and stores them in the ServletContext domain, and provides a client tool WebApplicationContextUtils for The user obtains the application context object.
So there are only two things we need to do:
① Configure the ContextLoaderListener listener in web.xml (import spring-web coordinates)
② Use WebApplicationContextUtils to obtain the application context object ApplicationContext
1.3 Import the coordinates of Spring integrated web
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
1.4 Configure ContextLoaderListener listener
<!--全局参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
1.5 Obtaining application context objects through tools
ApplicationContext applicationContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
Object obj = applicationContext.getBean("id");
Knowledge points
Spring integrated web environment steps
①Configure the ContextLoaderListener listener
②Use WebApplicationContextUtils to get the application context
2. Introduction to SpringMVC
2.1 Overview of SpringMVC
SpringMVC is a Java-based request-driven lightweight Web framework that implements the MVC design model. It is a follow-up product of SpringFrameWork and has been integrated into Spring Web Flow.
SpringMVC has become one of the most mainstream MVC frameworks, and with the release of Spring3.0, it has completely surpassed Struts2 to become the best MVC framework. It uses a set of annotations to make a simple Java class a controller for processing requests without implementing any interfaces. At the same time it also supports RESTful programming style requests.
2.3 SpringMVC quick start
Requirements: The client initiates a request, the server receives the request, executes the logic, and performs a view jump.
Development steps
①Import SpringMVC related coordinates
②Configure the SpringMVC core controller DispathcerServlet
③Create Controller class and view page
④ Use annotations to configure the mapping address of the business method in the Controller class
⑤Configure SpringMVC core file spring-mvc.xml
⑥The client initiates a request to test the
code
①Import the coordinates of Spring and SpringMVC, and import the coordinates of Servlet and Jsp
<!--Spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--SpringMVC坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--Servlet坐标-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--Jsp坐标-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
②Configure the core controller of SpringMVC in web.xml
<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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
③Create Controller and business method
public class QuickController {
public String quickMethod(){
System.out.println("quickMethod running.....");
return "index";
}
}
③Create the view page index.jsp
<html>
<body>
<h2>Hello SpringMVC!</h2>
</body>
</html>
④Configuration annotation
@Controller
public class QuickController {
@RequestMapping("/quick")
public String quickMethod(){
System.out.println("quickMethod running.....");
return "index";
}
}
⑤ Create spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置注解扫描-->
<context:component-scan base-package="com.itheima"/>
</beans>
⑥Visit the test address
http://localhost:8080/itheima_springmvc1/quick
SpringMVC development steps
①Import SpringMVC related coordinates
②Configure the SpringMVC core controller DispathcerServlet
③Create Controller class and view page
④ Use annotations to configure the mapping address of the business method in the Controller class
⑤Configure SpringMVC core file spring-mvc.xml
⑥The client initiates a request test
3. SpringMVC component analysis
3.1 SpringMVC execution process
3.3 SpringMVC annotation analysis
@RequestMapping
Role: used to establish the correspondence between the request URL and the method of processing the request
position:
On the class, the first level of the URL is requested to access the directory. If not written here, it is equivalent to the root directory of the application
In the method, the second-level access directory of the request URL is combined with the first-level directory marked with @ReqquestMapping on the class to form an access virtual path
Attributes:
value: Used to specify the requested URL. It has the same function as the path attribute
method: used to specify the request method
params: Used to specify the conditions for limiting the request parameters. It supports simple expressions. The key and value of the request parameter must be exactly the same as the configuration
E.g:
params = {"accountName"}, which means that the request parameter must have accountName
params = {"moeny!100"}, which means that the money in the request parameter cannot be 100
1. The mvc namespace is introduced
命名空间:xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
约束地址:http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
Component scan
SpringMVC is based on the Spring container, so when performing SpringMVC operations, you need to store the Controller in the Spring container. If you use the @Controller annotation, you need to use <context:component-scan base-package="com.itheima.controller"/ >Perform a component scan.
3.4 SpringMVC XML configuration analysis
SpringMVC has a default component configuration. The default components are configured in the DispatcherServlet.properties configuration file. The configuration file address is org/springframework/web/servlet/DispatcherServlet.properties. The default view parser is configured in this file, as follows:
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
Looking at the source code of the parser, you can see the default settings of the parser, as follows:
REDIRECT_URL_PREFIX = "redirect:" --重定向前缀
FORWARD_URL_PREFIX = "forward:" --转发前缀(默认值)
prefix = ""; --视图名称前缀
suffix = ""; --视图名称后缀
- View resolver
We can modify the prefix and suffix of the view through attribute injection
<!--配置内部资源视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3.5 Knowledge points
Related components of SpringMVC
Front controller: DispatcherServlet (responsible for calling other functional components)
Handler mapper: HandlerMapping (help with an address resolution and return an execution chain)
Processor adapter: HandlerAdapter (called by the front controller to help execute the processor)
Processor: Handler (equivalent to Controller)
View resolver: View Resolver (responsible for parsing out the view)
View: View (information about the package view)
Annotation and configuration of SpringMVC
Request mapping annotation: @RequestMapping
View resolver configuration:
REDIRECT_URL_PREFIX = “redirect:”
FORWARD_URL_PREFIX = “forward:”
prefix = “”;
suffix = “”;