入門案例分析:
1.執行流程
1.當啓動tomcat服務器時,由於配置了load_on_startup標籤,所以會創建DispatcherServlet對象,就會加載springmvc.xml配置文件
2.開啓了註解掃描,HelloController對象就會被創建
3.從index.jsp發送請求,請求會先到達DispatcherServlet核心控制器,根據配置@RequestMapping註解找到執行的具體方法
4.根據方法的返回值,再根據配置的視圖解析器,去指定的目錄下查找指定名稱的jsp文件
5.Tomcat服務器渲染頁面,做出響應
2.流程圖
3.RequestMapping註解
1.RequestMapping註解的作用是建立請求URL和處理方法之間的對應關係
2.RequestMapping註解可以作用在方法和類上
作用在類上:第一級的訪問目錄
作用在方法上:第二級的訪問目錄
細節:/表示應用的根目錄開始
${pageContext.request.contextPath}也可以省略不寫,但是路徑上不能寫/
3.RequestMapping屬性:
path:指定請求路徑的url
value:同path一樣
method:指定該方法的請求方式,是枚舉類型
params:指定限制請求參數的條件
headers:發送的請求中必須包含的請求頭
SpringMVC的入門程序案例
1.創建WEB工廠,引入開發的jar包,需要導入的座標如下
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<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>
2.配置核心的控制器(DispatcherServlet)
在web.xml配置文件中
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--springMVC的核心控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置Servlet的初始化參數哦,讀取springmvc的配置文件,創建spring容器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!--配置servlet啓動時加載對象-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.編寫springmvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--開啓註解掃描-->
<context:component-scan base-package="cn.ycl"></context:component-scan>
<!--視圖解析器對象-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--開啓springMVC框架對註解的支持-->
<mvc:annotation-driven/>
</beans>
4.編寫index.jsp和HelloController控制器類
1.index.jsp
<body>
<h3>入門程序</h3>
<a href="hello">入門程序</a>
</body>
2.HelloController
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,springMVC!!");
return "success";
}
}
5.在WEB-INF目錄下創建pages文件夾,編寫success.jsp的成功頁面
<h3>返回成功</h3>
6.啓動服務器進行測試