spring web mvc的羅曼蒂克

Spring Web MVC framework

  • The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files.
    Spring MVC框架是圍繞DispatcherServlet來設計的,DispatcherServlet會把請求分發給各個處理器,並支持可配置的處理器映射、視圖渲染、本地化、時區與主題渲染和文件上傳等功能。
  • handlers是註解了@Controller@RequestMapping的類和方法。

1. serlvet註冊

  • The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class), and as such is declared in the web.xml of your web application.
    DispatcherServlet是一個實際的Servlet(它繼承自HttpServlet基類),因此是在Web應用程序的web.xml中聲明的。
  • ·DispatcherServlet·是整個Spring MVC的核心。它負責接收HTTP請求組織協調Spring MVC的各個組成部分。其主要工作有:
    (1)截獲符合特定格式的URL請求。
    (2)初始化DispatcherServlet上下文對應WebApplicationContext,並將其與業務層、持久化層的WebApplicationContext建立關聯。
    (3)初始化Spring MVC的各個組成組件,並裝配到DispatcherServlet中。

Demo

  • 基於maven webapp的springMVC項目目錄

    其中,springmvc-servlet.xml就是該項目的
  • web.xml: 設置springmvc-servlet.xml的路徑
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

  • springmvc-servlet.xml
<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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="org.example.controller"/>
    <mvc:default-servlet-handler/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

2. IoC容器創建

Spring container

  • The Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete lifecycle from creation till destruction. These objects are called Spring Beans.
    Spring容器是Spring框架的核心。 容器將創建對象,將它們連接在一起,進行配置,並管理從創建到銷燬的整個生命週期。 這些對象稱爲Spring Bean。
  • The Spring container uses dependency injection (DI) to manage the components that make up an application.
    Spring容器使用依賴項注入(DI)來管理組成應用程序的組件。

Spring IoC Container: BeanFactory & ApplicationContext

  • BeanFactory is the simplest container providing the basic support for DI and is defined by the org.springframework.beans.factory.BeanFactory interface.
  • ApplicationContextContainer adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface.
  • The ApplicationContext container includes all functionality of the BeanFactory container, so it is generally recommended over BeanFactory.

Demo

  • web.xml
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/springmvcContext.xml</param-value>
  </context-param>
  • springmvcContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="..." class="...">
    <!-- collaborators and configuration for this bean go here -->
  </bean>

  <bean id="..." class="...">
    <!-- collaborators and configuration for this bean go here -->
  </bean>

  <!-- more bean definitions go here -->

</beans>

3. bean初始化

  • The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.
  • A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
  • These beans are created with the configuration metadata that you supply to the container.

Demo

  • Student.java
package org.example.model;
import java.util.Date;
public class Student {
    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  • 在ApplicationContext.xml中註冊爲bean
<bean id="student" class="org.example.model.Student">
        <property name="name" value="Jane"/>
    </bean>
  • 使用bean

自動裝配:@Autowired 註釋,它可以對類成員變量、方法及構造函數進行標註,完成自動裝配的工作。

@Controller
public class TestController {
    @Autowired
    private Student student;
    @RequestMapping(value = "hello")
    public String init(ModelMap modelMap){
        modelMap.addAttribute("name",student.getName());

        System.out.println(student.getName());
        return "hello";
    }
}
  • hello.jsp
<html>
<head>
    <title>hello</title>
</head>
<body>
hello,${name}
</body>
</html>
  • 運行截圖
    在這裏插入圖片描述

4. MVC處理用戶請求的流程

在這裏插入圖片描述
(1) 首先瀏覽器發送請求——>前端控制器DispatcherServlet,前端控制器收到請求後自己不進行處理,而是委託給其他的解析器進行處理,作爲統一訪問點,進行全局的流程控制;
(2) 尋找處理器:由DispatcherServlet控制器查詢一個或多個HandlerMapping,找到處理請求的Controller。
(3) 調用處理器:DispatcherServlet將請求提交到Controller。
(4)(5)調用業務處理和返回結果:Controller調用業務邏輯處理後,返回ModelAndView。
(6)處理視圖映射並返回模型: DispatcherServlet查詢一個或多個ViewResoler視圖解析器,找到ModelAndView指定的視圖。
(7) Http響應:視圖負責將結果顯示到客戶端。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章