Spring MVC 是一個模型 - 視圖 - 控制器(MVC)的Web框架建立在中央前端控制器servlet(DispatcherServlet),它負責發送每個請求到處理程序,使用視圖來給後臺返回響應結果的概念,用於前後端交互。
官方文檔:https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/web.html#spring-web
1、我的第一個HelloSpringMVC網頁
1.新建一個maven項目,添加web支持
2.pom.xml導入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springmvc01</groupId>
<artifactId>springmvc01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
</dependencies>
<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>
</project>
3.在web中註冊SpringMVC提供的DispatcherServlet
<?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">
<!--1.註冊DispatcherServlet Spring提供-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--3.關聯一個 SpringMVC配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--4.服務器啓動的時候就啓動-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--
2.註冊servlet的映射
/ 匹配所有請求 不包括.jsp
/* 匹配所有的請求,包括.jsp
-->
<servlet-mapping>
<servlet-name>DispatcherServlet</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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://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">
<!--固定配置-->
<!--1.掃描包配置讓下面的所有類被spring識別 控制層 和前端交互的 controller-->
<context:component-scan base-package="com.yang.controller"/>
<!--2.註解驅動開驅動-->
<mvc:annotation-driven/>
<!--3.在web開發中,我們一般存在靜態資源,css,js,img.. 我們不希望處理它-->
<mvc:default-servlet-handler/>
<!--視圖解析器 默認id:internalResourceViewResolver,這裏不要改,因爲以後會調用到-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前綴-->
<property name="prefix" value="WEB-INF/views/"/>
<!--後綴-->
<property name="suffix" value=".jsp"/>
<!--WEB-INF/views/.jsp-->
</bean>
</beans>
5.控制層編寫
package com.yang.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){
model.addAttribute("msg","Hello,SpringMVC");
System.out.println("劉洋最美");
return "hello";
}
}
6.編寫跳轉界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello</h1>
${msg}
</body>
</html>
7.配置tomcat測試看是否生效
結果:
前端:
後端:
需要注意的點:
需要把依賴導入lib目錄中,不然會出現404錯誤: