spring 笔记三 Spring与Web环境集成

文章目录

  • Spring与Web环境集成
    • ApplicationContext应用上下文获取方式
    • 导入Spring集成web的坐标
    • 置ContextLoaderListener监听器
    • 通过工具获得应用上下文对象
    • SpringMVC概述
    • SpringMVC快速入门

Spring与Web环境集成

ApplicationContext应用上下文获取方式

应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件) ,这样的弊端是配置文件加载多次,应用上下文对象创建多次。

在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了

Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:

① 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
② 使用WebApplicationContextUtils获得应用上下文对象ApplicationContext

导入Spring集成web的坐标

  
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>5.2.8.RELEASEversion>
    dependency>

置ContextLoaderListener监听器


 <context-param>
 <param-name>contextConfigLocationparam-name>
 <param-value>classpath:applicationContext.xmlparam-value>
 context-param>
 
 <listener>
 <listener-class>
 org.springframework.web.context.ContextLoaderListener
 listener-class>
 listener>

通过工具获得应用上下文对象

ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
 Object obj = applicationContext.getBean("id");

SpringMVC概述

SpringMVC 是一种基于Java 的实现MVC 设计模型的请求驱动类型的轻量级Web 框架,属于
SpringFrameWork 的后续产品,已经融合在Spring Web Flow 中。
SpringMVC 已经成为目前最主流的MVC框架之一,并且随着Spring3.0 的发布,全面超越Struts2,成为最优秀的MVC 框架。它通过一套注解,让一个简单的Java 类成为处理请求的控制器,而无须实现任何接口。同时它还支持RESTful编程风格的请求。

SpringMVC快速入门

需求:客户端发起请求,服务器端接收请求,执行逻辑并进行视图跳转。
开发步骤:
① 导入SpringMVC相关坐标
② 配置SpringMVC核心控制器DispathcerServlet
③ 创建Controller类和视图页面
④ 使用注解配置Controller类中业务方法的映射地址
⑤ 配置SpringMVC核心文件spring-mvc.xml
⑥ 客户端发起请求测试

SpringMVC流程图示
spring 笔记三 Spring与Web环境集成_第1张图片

  • 导入Spring和SpringMVC的坐标
<!--Spring坐标-->
 
 org.springframework
 spring-context
 5.2.8.RELEASE
 
 
 
 org.springframework
 spring-webmvc
 5.2.8.RELEASE
 
  • 导入Servlet和Jsp的坐标
  
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
      <scope>providedscope>
    dependency>
 
 <dependency>
    
    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>2.2version>
      <scope>providedscope>
    dependency>
  • 在web.xml配置SpringMVC的核心控制器
<servlet>
 <servlet-name>DispatcherServletservlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
 <init-param>
 <param-name>contextConfigLocationparam-name>
 <param-value>classpath:spring-mvc.xmlparam-value>
 init-param>
 <load-on-startup>1load-on-startup>
 servlet>
 <servlet-mapping>
 <servlet-name>DispatcherServletservlet-name>
 <url-pattern>/url-pattern>
 servlet-mapping>
  • 创建Controller和业务方法
public class QuickController {
 public String quickMethod(){
 System.out.println("quickMethod running.....");
 return "index";
 }
 }
  • 创建视图页面index.jsp
<html>
 <body>
 <h2>Hello SpringMVC!h2>
 body>
 html>
  • 配置注解
@Controller
 public class QuickController {
 @RequestMapping("/quick")
 public String quickMethod(){
 System.out.println("quickMethod running.....");
 return "index";
 }
 }
  • 创建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>
  • 访问测试地址
http://localhost:8080/itheima_springmvc1/quick 

页面显示
在这里插入图片描述

你可能感兴趣的:(spring,spring,笔记,前端,后端,java)