Java | Spring框架学习笔记--(4)MVC框架整合

系列文章:
Java | Spring框架学习笔记–(1)工厂
Java | Spring框架学习笔记–(2)AOP
Java | Spring框架学习笔记–(3)持久层整合
Java | Spring框架学习笔记–(4)MVC框架整合
Java | Spring框架学习笔记–(5)注解编程

MVC框架整合

为什么要整合MVC框架?

  1. MVC框架提供了控制器(Controller)调用Service
  2. 请求响应的处理
  3. 接受请求参数 request.getParameter(" ")
  4. 控制程序的运行流程
  5. 视图解析(JSP、JSON、Freemarker、Thyemeleaf)

环境搭建

  1. 新建一个Maven的项目/模块,骨架选择列表里的webapp

  2. 导入依赖

        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
    
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>javax.servlet.jsp-api</artifactId>
          <version>2.3.1</version>
          <scope>provided</scope>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>5.3.1</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.3.1</version>
        </dependency>
    
     	<dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>5.3.1</version>
         </dependency>
    

Spring整合MVC框架核心思路

  1. 如何准备工厂?

    //使用的是WebXmlApplicationContext而不再是ClassPathApplicationContext
    ApplicationContext applicationContext = new WebXmlApplicationContext("/applicationContext.xml")
    
  2. 如何保证工厂唯一、共用?
    共用:把ApplicationContext存放在ServletContext域中
    唯一:在ServletContext对象创建的同时创建ApplicationContext,因为ServletContext只会创建一次,所以ApplicationContext也就只有一个。使用ServletContextListener,就可以监听ServletContext的创建,把创建工厂的代码放在里面。

Spring已经封装了一个ContextLoaderListener,作用是为我们创建工厂并存放在ServletContext中。

在这里插入图片描述
在这里插入图片描述

ContextLoaderListener的使用方式:

由于他是一个监听器,所以在web.xml里配置即可。同时还要指定配置文件具体存放的位置!

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
	<param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

在Controller中获取ApplicationContext:

@RequestMapping("/hello")
public String hello(HttpServletRequest request){
    ServletContext context = request.getServletContext();
    ApplicationContext applicationContext = (ApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    System.out.println(applicationContext);
    return "success";
}

附1:完整WEB-INF/web.xml配置

<!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>
  <display-name>Archetype Created Web Application</display-name>

  <!-- SpringMVC的核心控制器 -->
  <servlet>

    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置Servlet的初始化参数,读取springmvc的配置文件,创建spring容器 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
      <!-- 自动加载xml文件,只有这里配置了才会加载下面的xml文件 -->
    </init-param>

    <load-on-startup>1</load-on-startup><!-- 配置servlet启动时加载对象 -->

  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>
      org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <!-- 设置过滤器中的属性值 -->
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <!-- 启动过滤器 -->
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <!-- 过滤所有请求 -->
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--以下是spring整合springmvc-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

</web-app>

附2:完整springmvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- 配置spring创建容器时要扫描的包 -->
    <context:component-scan base-package="com.prince"></context:component-scan>
    <!-- 配置视图解析器 -->
    <!--视图类的方法,返回值是String的时候,会自动跳转到某个页面,看下面怎么配置-->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <!-- 跳转到的文件的所在目录 -->
        <property name="suffix" value=".jsp"></property>
        <!-- 文件后缀名 -->
    </bean>
    <!-- 配置spring开启注解mvc的支持-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--
        通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器
        设置不过滤内容,比如:css,js,img 等资源文件
        location指的是本地的真实路径,mapping指的是映射到的虚拟路径。
        **表示这个文件夹下的所有资源
    -->
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>

    <!--解决跨域问题-->
    <mvc:cors>
        <mvc:mapping path="/**"
                     allowed-origin-patterns="*"
                     allowed-methods="POST, GET, OPTIONS, DELETE, PUT,PATCH"
                     allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-RequestedWith"
                     allow-credentials="true"/>
    </mvc:cors>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值