SpringMVC入門與環境搭建

shaonianbz發表於2018-04-02

一、什麼是MVC

1、Model1

這裡寫圖片描述

2、Model2

這裡寫圖片描述

二、SpringMVC概述

1、什麼是SpringMVC

(1)Springmvc是一個web層mvc框架,類似struts2

2、SpringMVC執行流程(重點)

這裡寫圖片描述

三、SpringMVC入門案例

1、匯入jar包

這裡寫圖片描述

2、配置web.xml

<!-- 配置前端控制器 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--預設載入配置檔案的規範: 
        檔案命名:servlet-name-servlet.xml    例如:springmvc-servlet.xml
        路徑規範:必須在WEB-INF目錄下
     -->
    <!-- 載入配置檔案 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

3、配置springmvc.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 配置處理器對映器,springmvc預設的處理器對映器BeanNameUrlHandlerMapping:
    根據(自定義Controler)的name屬性的url去尋找handler(Action) -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <!-- 配置處理器介面卡執行Controller,springmvc預設的是:SimpleControllerHandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!-- 自定義Controller -->
    <bean name="/hello.do" class="com.san.controller.MyController"/>

    <!-- 配置springmvc檢視解析器,解析邏輯檢視 
        後臺返回邏輯檢視:index
        檢視解析器解析出真正的物理檢視:字首+邏輯檢視+字尾===>/WEB-INF/jsps/index.jsp
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

4、自定義Controller

public class MyController implements Controller{

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView mv=new ModelAndView();
        //設定頁面回顯資料
        mv.addObject("hello","歡迎學習springmvc");
        //返回物理檢視
        mv.setViewName("index");
        return mv;
    }

}

6、根據程式碼分析springmvc執行過程

這裡寫圖片描述

相關文章