一、spring集成web环境
1.ApplicationContext应用上下文获取方式
应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,只是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这会导致配置文件加载多次,应用上下文对象创建多次。
在Web项目中,可以使用ServletContextListener监听Web应用的启动,如果在Web应用启动时就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
2.Spring提供的获取应用上下文的工具
Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
①.在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<!--全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
②.使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
二、springMVC组件及组件解析
客户端发起请求,服务器端接收请求,执行逻辑并进行视图跳转。
1.SpringMVC开发步骤:
①.导入SpringMVC相关坐标
<!--spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.13</version>
</dependency>
<!--springMVC坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</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>
②.配置SpringMVC核心控制器DispathcerServlet
<!--配置SpringMVC的前端控制器-->
<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>
③.创建Controller类和视图页面
public class QuickController {
public String quickMethod(){
System.out.println("quickMethod running.....");
return "index";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Success!</h1>
</body>
</html>
④.使用注解配置Controller类中业务方法的映射地址
@Controller
@RequestMapping("/user")
public class UserController {
// 请求地址 http://localhost:8080/user/quick
@RequestMapping(value="/quick",method = RequestMethod.GET,params = {"username"})
public String save(){
System.out.println("Controller save running....");
return "success";
}
}
⑤.配置SpringMVC核心文件 spring-mvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.alibaba.com/schema/stat"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">
<!--Controller的组件扫描-->
<context:component-scan base-package="com.itheima">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置内部资源视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- /jsp/success.jsp -->
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
⑥.客户端发起请求测试:访问测试地址
2.流程图示
3.执行流程
①.用户发送请求至前端控制器DispatcherServlet
②.DispatcherServlet收到请求调用HandlerMapping处理器映射器
③.处理器映射器找到具体的处理器(可以根据xml配置、注解进行查找),生成处理器对象及处理器拦截器(如果 有则生成)一并返回给DispatcherServlet
④.DispatcherServlet调用HandlerAdapter处理器适配器
⑤.HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)
⑥.Controller执行完成返回ModelAndView
⑦.HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
⑧.DispatcherServlet将ModelAndView传给ViewReslover视图解析器
⑨.ViewReslover解析后返回具体View
⑩.DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中),DispatcherServlet响应用户
4.组件解析
①. 前端控制器:DispatcherServlet
用户请求到达前端控制器,它就相当于MVC模式中的C,DispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户的请求,DispatcherServlet的存在降低了组件之间的耦合性。
②. 处理器映射器:HandlerMapping HandlerMapping
负责根据用户请求找到Handler即处理器,SpringMVC提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。
③. 处理器适配器:HandlerAdapter
通过HandlerAdapter对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。
④. 处理器:Handler
它就是我们开发中要编写的具体业务控制器。由DispatcherServlet把用户请求转发到Handler。由 Handler对具体的用户请求进行处理。
⑤. 视图解析器:View Resolver
View Resolver负责将处理结果生成View视图,View Resolver首先根据逻辑视图名解析成物理视图名,即具体的页面地址,再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。
⑥. 视图:View
SpringMVC 框架提供了很多的View视图类型的支持,包括:jstlView、freemarkerView、pdfView等。最常用的视图就是jsp。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。
5.注解解析
@RequestMapping
作用:用于建立请求URL和处理请求方法之间的对应关系。
位置:类上,请求URL的第一级访问目录。此处不写的话,就相当于应用的根目录。
方法上,请求URL的第二级访问目录,与类上的使用@ReqquestMapping标注的一级目录一起组成访问虚拟路径。
属性:value:用于指定请求的URL,它和path属性的作用是一样的。
method:用于指定请求的方式。
params:用于指定限制请求参数的条件,它支持简单的表达式。要求请求参数的key和value必须和配置的一模一样。
例如:params = {"accountName"},表示请求参数必须有accountName。
params = {"moeny!100"},表示请求参数中money不能是100。