关于java:Spring-MVC-二-基于xml配置

4次阅读

共计 4668 个字符,预计需要花费 12 分钟才能阅读完成。

创立一个基于 xml 配置的 Spring MVC 我的项目。

Idea 创立新我的项目,pom 文件引入依赖:

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.12.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.12.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.12.RELEASE</version>
    </dependency>

在我的项目的 webapp/WEB-INF 目录下创立 web.xml 文件,配置 DispatcherServlet:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <servlet>
    <description>dispatcherServlet</description>
    <display-name>DispatcherServlet</display-name>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.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>

留神其中定义了 contextConfigLocation:上下文配置文件地位,配置为:classpath:springmvc.xml,指定 Spring MVC 启动过程中会到指定地位读取该配置文件,所以咱们须要配置好 springmvc.xml 文件。

在我的项目 resource 门路下配置 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">

    <!-- 扫描包 -->
    <context:component-scan base-package="org.example.controller"/>
    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 视图前缀 -->
        <property name="prefix" value="/" />
        <!-- 视图后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

次要是指定 Spring MVC 的 controller 的包扫描门路,并指定视图解析器、以及视图后缀为 jsp。

最初,创立一个 conrolller:

package org.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {@GetMapping("/hello")
    @ResponseBody
    public String hello(ModelAndView model){return "<h1>Hello World</h1>";}
}

咱们的目标就是让疾速让这个 Spring MVC 案例跑起来,就不创立 jsp 文件了,所以在 controller 中引入了 @ResponseBody 注解(不加这个注解的话,controller 的 @GetMapping 办法返回 String 则视图解析器会将其解决为 jsp 页面)。

我的项目构造如下图:

我的项目启动配置:

启动后,测试:

所以,通过 idea 创立一个基于 SpringMVC 的我的项目非常简单,通过以上几步,一个 Spring MVC 我的项目就能够失常运行起来了。

接下来咱们先简略钻研一下以上案例得以失常运行的底层原理。

DispatcherServlet

咱们晓得 Spring MVC 是通过一个外围 servlet:DispatcherServlet 来解决申请的,所以还是从 DispatcherServlet 动手。

先来看一眼 DispatcherServlet 类的继承关系:

这个类层次结构中,Servlet->GenericServlet->HttpServlet 是属于 java SDK 的内容,来自于 javax.servlet.http 包,之后的从 HttpServletBean 到 DispatcherServlet 是属于 Spring Web MVC 框架的内容,来自于 org.springframework.web.servlet 包。

如果不应用 Spring Mvc、本人创立 Servlet 的话,咱们的 Servlet 间接继承 HttpServlet 即可,其实实现起来也很简略:
1. 创立 MyServlet

package org.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write("<h1>OHhhhh,this is my own Servlet...</h1>");
    }
}

2. 在 web.xml 中配置 MySevlet

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>org.example.servlet.MyServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/my</url-pattern>
  </servlet-mapping>

3. 启动我的项目,拜访

其实 Spring MVC 的工作原理和 MyServlet 的工作原理相似,MyServlet 是通过配置文件找到以后申请 URL 对应的 Servlet,而后再依据申请类型调用 Servlet 的 doGet 或者 doPost 等办法解决申请。Spring MVC 解决申请的 Servlet 是 DispatcherServlet,DispatcherServlet 依据申请 URL 匹配 HandlerMapping、找到对应的 Controller,之后交给 HandlerAdapters 通过申请参数的匹配、转换之后,通过反射机制调用 Controller 的对应办法,实现申请的解决。

上述 DispatcherServlet 的底层解决逻辑,前面咱们会逐渐细化、深刻,从代码层面进行剖析。

上一篇 Spring MVC 一:从 MVC & Servlet 开始

正文完
 0