轉(zhuǎn)載請(qǐng)注明來(lái)源 賴賴的博客
導(dǎo)語(yǔ)
了解一下起因經(jīng)過(guò),你才能明白世事的真正含義。
Spring的基礎(chǔ)都了解了一些唇兑,這一篇就來(lái)看看Spring應(yīng)用MVC會(huì)是怎么樣的吧坎弯,輕松簡(jiǎn)單纺涤,本章講述的是一次訪問(wèn)開(kāi)始到返回Spring MVC是如何應(yīng)對(duì)的(基礎(chǔ)版)
實(shí)例
項(xiàng)目工程目錄結(jié)構(gòu)和代碼獲取地址
獲取地址(版本Log將會(huì)注明每一個(gè)版本對(duì)應(yīng)的課程)
https://github.com/laiyijie/SpringLearning
目錄結(jié)構(gòu)
如圖所示,雖然目錄結(jié)構(gòu)看起來(lái)復(fù)雜了一些(就是文件夾深度深了一點(diǎn)兒而已)抠忘,其實(shí)最源文件只有四個(gè)撩炊。所以不要緊張,這非常簡(jiǎn)單
運(yùn)行工程
運(yùn)行方式
- 右鍵整個(gè)項(xiàng)目
- Run as
- Run On Server
- 選擇STS默認(rèn)的 Pivotal Server(有tomcat也一樣)并確認(rèn)
- 在瀏覽器里輸入 http://localhost:8080/demo/hi (demo是工程的名稱(chēng)崎脉,依照你的來(lái)替換)
運(yùn)行結(jié)果
我用的是 chrome瀏覽器拧咳,輸出了hello
OK,這次從瀏覽器輸入這個(gè)鏈接到返回這個(gè)hello來(lái)講解囚灼!
項(xiàng)目詳解
我們不妨直接找找這個(gè)輸出的 hello
來(lái)自哪一段代碼
UserController.java
package me.laiyijie.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@RequestMapping("/hi")
@ResponseBody
public String hello(){
return "hello";
}
}
是不是很短骆膝,簡(jiǎn)潔明了祭衩!
我們一眼看到的就是 hello
!沒(méi)錯(cuò)阅签,就是這個(gè)函數(shù)里面處理了剛才的請(qǐng)求掐暮,并且返回了一個(gè)hello
的字符串!
這里有三個(gè)注解含義分別如下:
- @Controller 將一個(gè)類(lèi)變成一個(gè)特殊的
java bean
作為控制器使用政钟,可以認(rèn)為是特殊的@Service
- @RequestMapping 這里面就是針對(duì)URL的控制路克,我們可以看到訪問(wèn)的URL/demo為項(xiàng)目名稱(chēng),而/hi就代表的是訪問(wèn)由這個(gè)函數(shù)來(lái)控制养交!
- @ResponseBody 將
hello
這個(gè)字符串直接返回給瀏覽器(不增加這個(gè)注解的情況下是返回一個(gè)對(duì)應(yīng)的jsp頁(yè)面-例如hello.jsp)
進(jìn)入到/demo以后是由@RequestMapping
控制這個(gè)訪問(wèn)精算,那么一個(gè)訪問(wèn)是怎么被引到如到demo這個(gè)工程中的呢?Spring Context又是怎么生效的呢碎连?(我們并沒(méi)有用new ClassPathXmlApplicationContext("root-context.xml");
來(lái)初始化一個(gè)Spring Context灰羽,為何注解就用了起來(lái)?)
瀏覽器中輸入 http://localhost:8080/demo/hi 并訪問(wèn)
- 瀏覽器訪問(wèn) localhost 的 8080 端口
- localhost的8080端口由Pivotal Server(或者tomcat鱼辙,剛才你運(yùn)行的時(shí)候開(kāi)啟的一個(gè)程序)監(jiān)聽(tīng)
- Server 接收到瀏覽器的請(qǐng)求并查看到路徑為/demo谦趣,轉(zhuǎn)交到demo工程來(lái)管理
- 訪問(wèn)進(jìn)入demo 這個(gè)工程后會(huì)先根據(jù) web.xml 來(lái)確認(rèn)入口是哪兒(這是tomcat等web容器一個(gè)通用的標(biāo)準(zhǔn))
那讓我們?cè)敿?xì)看一下web.xml是怎樣的!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_1.xsd"
version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee">
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
如果你不熟悉web.xml的配置座每,也請(qǐng)不要緊張前鹅,因?yàn)檫@部分的配置已經(jīng)非常成熟,一次配置完畢會(huì)很少改動(dòng)峭梳,即使你不太了解也不會(huì)影響你的使用舰绘,這里擺出只是為了讓你了解一個(gè)訪問(wèn)是如何進(jìn)入Spring MVC進(jìn)行管理的。
我們看關(guān)鍵一點(diǎn):
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
這里載入了一個(gè)配置文件葱椭,這個(gè)配置文件的名稱(chēng)是 servlet-context.xml 這么熟悉的名稱(chēng)捂寿,正是SpringContext的配置文件!
是的孵运,正是通過(guò)web.xml我們初始化了一個(gè)SpringContext秦陋,也就是tomcat(或者Pivotal Server)幫助我們初始化了這個(gè)SpringContext代替掉我們?cè)賛ain函數(shù)中使用的:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml");
Spring 容器初始化完畢,訪問(wèn)完全轉(zhuǎn)入Spring MVC 中由我們進(jìn)行控制治笨!
為了讓注解生效必然要配置一下 servlet-context.xml
servlet-context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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-4.3.xsd">
<context:component-scan base-package="me.laiyijie.demo" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"
p:messageConverters-ref="stringHttpMessageConverter" />
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter" />
</beans>
重點(diǎn)只有四句話:
- 開(kāi)啟注解驳概,掃描所有注解的組件(使得
@Controller
可以被實(shí)例化)
<context:component-scan base-package="me.laiyijie.demo" />
- 開(kāi)啟mvc注解驅(qū)動(dòng),使得
@RequestMapping
有效
<mvc:annotation-driven />
- 最后兩句配置適配器使得返回的
hello
正常輸出
pom.xml
<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>me.laiyijie</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
依賴新增一個(gè):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
小結(jié):
輸入 http://localhost:8080/demo/hi 訪問(wèn)全程:
- 訪問(wèn)
localhost
服務(wù)器的8080
端口 - 服務(wù)器監(jiān)聽(tīng)
8080
端口的webserver接收到請(qǐng)求 - 通過(guò)URL發(fā)現(xiàn)請(qǐng)求應(yīng)該轉(zhuǎn)入
/demo
這個(gè)工程處理 - 請(qǐng)求通過(guò)標(biāo)準(zhǔn)配置
web.xml
確認(rèn)了接受請(qǐng)求的程序?yàn)?appServelet - appServelet啟動(dòng)了一個(gè)由
servlet-context.xml
配置的Spring Context -
servlet-context.xml
配置了由注解驅(qū)動(dòng)旷赖,也就是通過(guò)@RequestMapping
來(lái)確認(rèn)訪問(wèn)應(yīng)該分配到哪兒 -
UserController
中的hello
方法配置了@RequestMapping("/hi")
也就是說(shuō)顺又,該請(qǐng)求由這個(gè)函數(shù)處理,函數(shù)處理請(qǐng)求完畢返回hello
等孵,hello這個(gè)字符串通過(guò)servlet-context.xml
中配置的轉(zhuǎn)換器轉(zhuǎn)換后輸出到瀏覽器