SpringMVC學習(二)使用註解開發SpringMVC

使用註解開發SpringMVC的步驟

  1. 新建一個Maven項目,添加web支持
    2.
  2. 處理Maven靜態資源的過濾問題,完善Maven配置
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
  1. 導入相關的Maven依賴
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
  1. 配置web.xml
  • 註冊DisPatchServlet
  • 關聯SpringMVC的核心配置文件
  • 設置啓動級別1,隨着服務器一起啓動
  • 攔截所有的請求: /
<?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">

    <!--註冊DispatchServlet-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--初始化時加載關聯SpringMVC的配置文件-->
        <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攔截-->
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
  1. 添加SpringMVC配置文件Springmvc-servlet.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: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
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--自動掃描controller下的所有的包-->
    <context:component-scan base-package="com.zyh.controller"/>
    <!--Spring不在處理靜態資源-->
    <mvc:default-servlet-handler/>
      <!--支持SpringMVC註解驅動
        在Controller中使用@RequestMapping註解完成映射關係
        要想使@RequestMapping註解生效
        必須向上下文中註冊DefaultAnnotationHandlerMapping和一個AnnotationMethodHandlerAdapter實例
        這兩個實例分別在類級別和方法級別處理。
        這兩個實例會被  <mvc:annotation-driven/> 自動幫我們完成這兩個實例的注入
    -->
    <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>
  1. 創建一個HelloController,類上添加註解@Controller

類中的兩個註解和參數:
@Controller:不用再去實現Controller接口,爲了讓SpringIOC容器初始化時掃描到
@RequestMapping("/hello"):表示真實的URL訪問地址爲項目名/hello
Model:把數據傳遞到視圖中

package com.zyh.controller;

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

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model) {
        //封裝數據給jsp
        model.addAttribute("msg","HelloSpringMVCAnnotation");
        return "hello"; //被視圖解析器處理去拼接靜態資源的位置 WEB-INF/jsp/hello/.jsp
    }
}
  1. 創建視圖層:在web/WEB-INF/jsp下創建一個hello.jsp
    在這裏插入圖片描述
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--讀取Controller中的數據--%>
${msg}
</body>
</html>
  1. 啓動Tomcat服務器,測試:
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章