天天看點

注解開發springmvc(SpringMVC學習筆記四)

1、建立一個Module,添加web支援

2、導入相關jar包,以及項目結構下的WEB-INF包下的lib包

3、編寫web.xml,注冊DispatcherServelt(固定不變的)

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    <!--配置DispatcherServlet這是SpringMVC的核心——>前端控制器(請求分發器)-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--DispatcherServlet要綁定spring的配置檔案(springmvc也屬于spring)-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--啟動級别:1  和伺服器同級(最高)-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--在springmvc中
    /:隻比對所有請求,不會比對jsp頁面
    /*:不僅比對所有請求,還會比對jsp界面,(用這個會出錯,因為我們設定了通過請求去跳轉jsp視圖。不能直接通路)
    -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
           

4、編寫springmvc配置檔案(固定不變的,但是要注意注解掃描包的路徑)

<?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: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">

    <!--自動掃描包,讓指定包下的注解生效,由IOC容器統一管理(spring就是IOC容器之一)-->
    <context:component-scan base-package="com.ming.controller"/>

    <!--讓spring mvc不處理靜态資源 預設開啟的,這裡不過是顯示定義了一下 防止過濾平時需要引用的資源   如:.css .js .html .mp3 .mp4-->
<!--    <mvc:default-servlet-handler/>-->

    <!--
        添加注解驅動支援
        在spring中一般使用@RequestMapper來完成映射關系
        要想使@RequestMapper注解生效
        必須在上下文中注冊DefaultAnnotationHandlerMapper
        和一個AnnotationMethodHandlerAdapter執行個體
        這兩個執行個體分别在類級别和方法級别處理
        而annotation-driver配置幫助我們自動完成上述兩個執行個體的注入
    -->
    <mvc:annotation-driven/>

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

</beans>
           

5、建立對應的控制類,controller

package com.ming.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Ming
 * @create 2021-08-21-0:37
 */
//通過注解配置bean控制器
@Controller
public class HelloController {

    //通過注解請求到url真實通路位址:項目名/類名/hello
    @RequestMapping("/hello")
    public String hello(Model model){
        //封裝資料:向模型中添加屬性msg與值,可以在jsp頁面中取出并渲染
        model.addAttribute("msg","HelloSpringMVCAnnotation!");
        //  /WEB-INF/jsp/hello.jsp
        return "hello";//會被視圖解析器處理
    }


    //要寫多個映射請求,直接通過注解添加映射即可(方法)
    //通過注解請求到url
//    @RequestMapping("/hello")
//    public String hello1(Model model){
//        //封裝資料:
//        model.addAttribute("msg","HelloSpringMVCAnnotation!");
//
//        return "hello";//會被視圖解析器處理
//    }
//    //通過注解請求到url
//    @RequestMapping("/hello")
//    public String hello2(Model model){
//        //封裝資料
//        model.addAttribute("msg","HelloSpringMVCAnnotation!");
//
//        return "hello";//會被視圖解析器處理
//    }
}
           

6、完善前端視圖和controller之間的對應

注解開發springmvc(SpringMVC學習筆記四)

7、tomcat打包測試運作

注解開發springmvc(SpringMVC學習筆記四)

 小結:

  • 使用springmvc必須配置三大件
    • 處理器映射器
    • 處理器擴充卡
    • 視圖解析器

通常,使用注解開發,隻需要手動配置視圖解析器,而處理器映射器和處理器擴充卡隻需要開同注解驅動即可,生氣大量的xml配置