文章目录
1.spring集成web环境
1.1ApplicationContext应用上下文获取方式
应用上下文对象是通过new ClassPathXmlApplicationContext(spring配置文件)方式获取的,但是每次从容器中获取Bean时都要编写new ClassPathXmlApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次
在Web项目中可以使用ServletContextListener监听web应用的启动,我们可以在web应用启动时,就加载spring的配置文件,创建应用上下文、对象ApplicationContext,在将其存储到最大的域ServletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
步骤
1.创造一个监听器
public class ContextLinister implements ServletContextListener {
/**
* 初始化方法
* @param sce
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("app.xml");
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("applicationContext",applicationContext);
}
/**
* 销毁方法
* @param sce
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
web.xml添加监听器
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>com.blb.Listener.ContextLinister</listener-class>
</listener>
</web-app>
获取ApplicationContext上下文
@WebServlet("/one")
public class one extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = req.getServletContext();
ApplicationContext applicationContext = (ApplicationContext)servletContext.getAttribute("applicationContext");
UserSeriver userSeriver =(UserSeriver) applicationContext.getBean("userSeriver");
userSeriver.say();
}
}
实现解耦
web.xml添加代码:
<web-app>
<context-param>
<param-name>app</param-name>
<param-value>app.xml</param-value>
</context-param>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>com.blb.Listener.ContextLinister</listener-class>
</listener>
</web-app>
监听器端
public class ContextLinister implements ServletContextListener {
/**
* 初始化方法
* @param sce
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
//读取web.xml中的全局参数
String app = servletContext.getInitParameter("app");
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(app);
//将上下文对象存储到ServletContext中
servletContext.setAttribute("applicationContext",applicationContext);
}
/**
* 销毁方法
* @param sce
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
1.2Spring提供获取应用上下文工具
上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载spring配置文件,创建应用上下文对象,并存储到ServContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获取上下文对象
所以现在我们需要做两件事情:
- 在web.xml中配置ContextLoaderListenerj监听器(导入spring-Web坐标)
- 使用WebApplicationContextUtils获取应用上下文对象ApplicationContext
1.xml配置
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- <listener-class>com.blb.Listener.ContextLinister</listener-class>-->
<!-- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
</listener>
</web-app>
web端
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// ServletContext servletContext = req.getServletContext();
// ApplicationContext app =(ApplicationContext) servletContext.getAttribute("app");
ApplicationContext app = WebApplicationContextUtil.getWebApplicationContext(servletContext);
// UserSeriver userSeriver =(UserSeriver) app .getBean("userSeriver");
// userSeriver.say();
ServletContext servletContext = req.getServletContext();
ApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserSeriver userSeriver =(UserSeriver) webApplicationContext.getBean("userSeriver");
userSeriver.say();
}
2.spring
2.1spring MVC简介
Spring MVC是一种基于java的实现MVC设计模式的请求驱动类型的轻量级web框架,属于SpringFrameWork的后续产品,已经融合在Spring Web Flow中
Spring MVC已经成为目前最主流的MVC框架之一,并且随着Spring3.0的发布,全面超越Struts2,成为最优秀的MVC框架。它通过一套注解,让一个简单的java类成为处理请求的控制器,而无需实现任何接口,同时他还支持RESTful编程风格的请求
2.2Spring MVC快速入门
需求:客户端发出请求,服务器端接收请求,执行逻辑并进行视图跳转
开发步骤:
- 导入Spring-MVC相关坐标
- 配置Spring-MVC核心控制器DispatcherServlet
- 创建Controller类和视图页面
- 使用注解配置Controller类中业务方法和映射地址
- 配置SpringMVC核心文件spring-mvc.xml
- 客户端发起请求
导入Spring-MVC相关坐标(pow.xml)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
配置Spring-MVC核心控制器DispatcherServlet
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
创建Controller类和视图页面
@Controller
public class UserController {
@RequestMapping("/quick")
public String save()
{
System.out.println("Controller Running");
return "success.jsp";
}
}
使用注解配置Controller类中业务方法和映射地址
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- <listener-class>com.blb.Listener.ContextLinister</listener-class>-->
<!-- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
</listener>
<!-- 配置spring-MVC前端控制器-->
<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>
</web-app>
配置SpringMVC核心文件spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
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" >
<!--加载相关文件 -->
<context:component-scan base-package="com.blb.Control"></context:component-scan>
</beans>
3.SpringMVC组件解析
3.1.SpringMVC的执行流程
- 用户发送请求至前端控制器DispatcherServlet
- DispatcherServlet收到请求调用handlerMapping处理器映射器
- 处理器映射器找到具体的处理器(可根据xml配置、注解进行查找),生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet
- DispatcherServlet调用HandlerAdapter处理器适配器
- HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)
- Controller执行完成后返回ModelAndView
- HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet
- DispatcherServlet将ModelAndView传给ViewReslover视图解析器
- ViewReslover解析后返回具体View
- DispatcherServlet根据View进行渲染视图(即将模型数据填充到视图中)DispatcherServlet响应用户
3.2 Spring MVC注解解析
@RequestMapping
作用:用于建立请求URL和处理请求方法之间的对应关系
位置:
- 类上:请求URL的第一级访问目录。此处不写的话,就相当于应用的根目录
- 方法上:请求URL的第二级访问目录,与此类上的使用@RequestMapping标注的一级目录一起组成访问虚拟路径
属性: - value:用于指定请求的URL。它和path属性的作用是一样的
- method::用于指定请求的方式
- params:用于指定限制请求参数的条件,它支持简单的表达式。要求请求的参数的key和value必须和配置的一模一样
例如:
params={“accountName”},表示请求的参数必须有accountName
params={“money!100”},表示请求的参数中money不能是100
请求方式
@Controller
public class UserControl {
@RequestMapping("/quick")
public String say()
{
System.out.println("hollow Spring-MVC");
// return "forward:/success.jsp";//重定向
return "redirect:/success.jsp";//转发
}
}
前缀、后缀
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="re"></property>
<property name="suffix" value="afr"></property>
</bean>
mvc命名空间引入
命名空间:xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=“http://www.springframework.org/schema/context”
约束地址
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”>
<context:component-scan base-package=“com.blb”></context:component-scan>
组件扫描
SpringMVC基于Spring容器,所以在进行SpringMVC操作时,需要将Controller存储到Spring‘容器中,如果使用@Controller注解标注的话,就需要使用 <context:component-scan base-package=“com.blb”></context:component-scan>进行组件扫描
3.3知识要点
3.3.1SpringMVC的相关组件
- 前端控制器:DispatcherServlet
- 处理映射器:HandlerMapping
- 处理器适配器:handlerAdapter
- 处理器:Handler
- 视图解析器:ViewResolve
- 视图:View
3.3.2SpringMVC的注解和配置
请求映射注解:@RequestMapping