Spring MVC 覆盤 | 工做原理及配置

一、Sping MVC 工做原理

舊文提過,再也不贅述。請務必通讀如下文章:html

https://mp.weixin.qq.com/s/z-...前端

二、 IDEA 建立 web 項目

建立項目

填充信息

原始項目

項目配置:java

建立 java 文件夾,放主要代碼

建立 resource 文件夾,放配置文件

指定爲 web 項目

重要配置

最終項目結構

詳細配置見:http://www.javashuo.com/article/p-ghulwwdi-he.htmlgit

三、Spring MVC Hello World

IDEA 建立項目並配置:github

  • 建立一個名爲 mvc-hello 的動態 Web 項目,並在建立的項目中的 src 文件夾下建立一個包 com.nasus.hello。
  • 加入 web 相關 pom 依賴。
  • 在 com.nasus.hello 包下建立一個 HelloController 類。
  • 在 webapp/WEB-INF 文件夾下建立 Spring 配置文件 web.xml 和 hello-servlet.xml。
  • 在 webapp/WEB-INF 文件夾下建立一個名爲 jsp 的子文件夾。
  • 在此子文件夾下建立視圖文件 hello.jsp。

建立項目並配置

pom 依賴web

<!-- 1.Spring核心依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <!-- 3.Spring web依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>

HelloController 類:spring

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String sayHello(ModelMap model) {
        model.addAttribute("data", "Hello World");
        return "hello";
    }

}

HelloController 指定了映射路徑,以及 http 訪問方式。並返回一個 ModelMap 和 hello 以供視圖解析器解析。瀏覽器

web.xmltomcat

<web-app id="WebApp_ID" 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">

  <display-name>Spring MVC Application</display-name>

  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <!-- 加載順序 -->
    <load-on-startup>1</load-on-startup>
  </servlet>

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

</web-app>

web.xml 定義一個前端控制器 DispatcherServlet ,servlet-name 爲 hello 。url-pattern 標籤訂義這個 servlet 將攔截全部的 url ,而 SpringMVC 默認將會從 webapp/WEB-INF 文件夾下加載 servlet-name 標籤指定的名稱 + -servlet.xml 格式的 servlet 配置文件。這裏加載的 servlet 配置文件正是 hello-servlet.xml 。mvc

hello-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       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-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 自動掃描 -->
    <context:component-scan base-package="com.nasus.hello" />

    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

作了兩件事,一是指定這個 servlet 自動掃描哪些包,並激活帶 web 相關注解,@Controller、@RequestMapping 的類。

InternalResourceViewResolver 是視圖解析器,它定義瞭解析視圖名稱的規則。根據上面定義的規則,hello 的邏輯視圖將委託給位於 /WEB-INF/jsp/hello.jsp 這個視圖來實現。

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
    <h2>${data}</h2>
</body>
</html>

HelloController 返回的視圖名稱 "hello",匹配到了 hello.jsp 。同時,hello.jsp 還取得返回的 model 中的內容。最後再由 DispatcherServlet 渲染視圖,返回瀏覽器,呈現給用戶。

訪問 http://127.0.0.1:8080/hello](http://127.0.0.1:8080/hello ,效果以下:

頁面效果

四、一些 Bug

IDEA 使用 tomcat 中文亂碼問題,解決方法:
http://www.javashuo.com/article/p-dfvikuwr-dx.html

IDEA 編譯時出現,javacTask: 源發行版 1.8 須要目標發行版 1.8,解決方法:
http://www.javashuo.com/article/p-bgcfahjt-es.html
https://www.cnblogs.com/benjieqiang/p/11298294.html

五、源碼地址

https://github.com/turoDog/review_spring

本文主要覆盤了 Spring MVC 的工做原理,建立了一個 Spring MVC 項目來輔助理解原理,具體更多 Spring MCV 的用法,請見後續文章。

推薦閱讀:

一、java | 什麼是動態代理

二、Spring 覆盤(1) | IOC

三、Spring 覆盤(2) | AOP

四、SpringBoot | 啓動原理

5、SpringBoot | 自動配置原理

一個優秀的廢人

相關文章
相關標籤/搜索