項(xiàng)目需求
項(xiàng)目需求
1)瀏覽器請求進(jìn)入index.js
2)index.jsp根據(jù)路勁請求服務(wù)器跳轉(zhuǎn)hello.jsp
SpringMVC搭建
1)基本目錄結(jié)構(gòu)
創(chuàng)建Maven項(xiàng)目
項(xiàng)目名稱
Maven倉庫配置
項(xiàng)目路結(jié)構(gòu)
1)web項(xiàng)目路結(jié)構(gòu)充完整
2)右鍵新創(chuàng)建的文件夾 --》mark Directory as (一個(gè)源碼文件 一個(gè)資源目錄)
2) Maven導(dǎo)入架包(pom.xml)
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 編譯環(huán)境 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- 定義Spring版本號 -->
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
3) 配置前端控制器(web.xml)
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<!-- 加載Spring DispatcherServlet 類-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 同時(shí)加載SpringMVC配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--
<load-on-startup>1</load-on-startup>
1)load-on-startup 元素標(biāo)記容器是否在啟動(dòng)是就加載這個(gè)Servlet(實(shí)例化并調(diào)用其init()方法)
2)它的值必須是一個(gè)整數(shù)
3)它的值大于0或等于0的整數(shù)表示在項(xiàng)目啟動(dòng)時(shí)就加載并初始化這個(gè)servlet
4)當(dāng)它的值小于0或未填寫時(shí)瞻讽,表示該servlet在被選擇是才會(huì)去加載并初始化
5)它的數(shù)值大于等于0時(shí),數(shù)值越低刚照,表示他被加載初始化的優(yōu)先級越高
6)正值相同時(shí)皿渗,容器會(huì)自動(dòng)選擇順序加載
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!--
/ 任意請求都會(huì)經(jīng)過Servlet
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
4) 在resouces目錄下新建一個(gè)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: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">
<!-- 開啟注解掃描 -->
<context:component-scan base-package="work.chenc.*"></context:component-scan>
<!-- 開啟視圖解析器
試題解析器對象:class 是固定的
-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 轉(zhuǎn)發(fā)是尋找指定目錄 -->
<property name="prefix" value="WEB-INF/pages/"></property>
<!-- 尋找文件的后綴文件名 這里是 .jsp文件 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 開啟SpringMVC注解支持 - 可暫不開啟 -->
<mvc:annotation-driven/>
</beans>
5) 在java文件下創(chuàng)建一個(gè)控制器
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(path = "/hello")
public String sayHello() {
System.out.println("Hello ");
return "hello";
}
}
6) webapp下的index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>首頁</h3>
<a href="hello">跳轉(zhuǎn)hello頁面</a>
</body>
</html>
7) 在WEN-INF新建一個(gè)pages文件夾同時(shí)在該文件夾下新增一個(gè)hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>say hello</h2>
</body>
</html>
8) 配置Tomcat
ps:配置Tomcat將不做贅述烟勋,可自行百度
9)啟動(dòng)Tomcat訪問index.jsp
index.jsp
10)點(diǎn)擊 - 跳轉(zhuǎn)hello頁面
hello.jsp
控制臺打印結(jié)果
10)整個(gè)請求執(zhí)行流程圖
流程圖
通過流程圖可知DipatcherServlet(前端控制器)相當(dāng)于一個(gè)調(diào)度器,所有的請求都會(huì)通過來找到對應(yīng)的映射凌外,從而進(jìn)行進(jìn)一步處理末早,把最終處理的界面再返還給DipatcherServlet烟馅,再由DipatcherServlet返回給瀏覽器