為什么需要Spring MVC
最開始接觸網頁的時候,是純的html/css頁面,那個時候還是用Dreamweaver來繪制頁面。
隨著網站開發的深入,開始學習servlet開發,記得最痛苦的就是servlet返回網頁的內容是字符串拼接的html頁面,整不好就無法顯示....
再到后來開學學習SSH,龐大的架構眼花繚亂。Struts繁雜的標簽、hibernate搞不清楚的數據表,Spring不知道哪里搞錯的bean。
最后隨著發展,前端開始占有一席之地,nodejs風生水起,很多業務邏輯開始前置。再也看不到當初的bo、dao了,取而代之的是各種框架的mvvm,后臺減輕壓力只負責一些必要的邏輯。
到現在,好像web開發又發展到了一個階段——前端由于Nodejs的作用,可以支撐一部分業務邏輯,通過轉發代理,統一發往后臺。后臺通過url實現mvc,對性持久化、更深入的邏輯操作等等。Spring MVC在這里就起了很關鍵的作用....它通過Url攔截請求,自定義業務邏輯,可以返回自定義的view或者模型數據。
當然,上面的鬼扯都是片面的,不代表行業的發展,只是博主管中窺豹而已。
下面步入正題,說說Spring MVC的最小化配置,給入門的朋友引個路。
Spring MVC的最小化配置
需要的jar包
- Spring framework spring-context
- Spring framework spring-mvc
具體可以參考maven中的引用:
1
2
3
4
5
6
7
8
9
10
|
< dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >4.2.4.RELEASE</ version > </ dependency > |
web.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app version = "3.1" 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_3_1.xsd"> < context-param > < param-name >contextConfigLocation</ param-name > < param-value >/WEB-INF/applicationContext.xml</ param-value > <!-- 默認是/WEB-INF/applicationContext.xml --> </ context-param > < listener > < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > < servlet > < servlet-name >SpringMVC</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value >/WEB-INF/SpringMVC-servlet.xml</ param-value > <!-- 默認是/WEB-INF/[servlet名字]-servlet.xml --> </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >SpringMVC</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > </ web-app > |
其中,必要的配置就是指定servlet和listener.
- ContextLoaderListener指定了IOC容器初始化的方法
- DispatcherServlet則定義了mvc的相關內容,并配置攔截的url,如上面所示,所有/開頭的請求,都會通過SpringMVC這個servlet進行處理。
他們都需要一個xml文件,默認位置上面已經說過了。
applicationContext.xml
空的,反正咱也沒用什么bean。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:context = "http://www.springframework.org/schema/context" xmlns:tx = "http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> </ beans > |
SpringMVC-servlet.xml
里面放一個掃描controller的配置即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? 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:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 設置使用注解的類所在的jar包 --> < context:component-scan base-package = "hello" /> </ beans > |
controller文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package hello; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping("/hello") public @ResponseBody String test() { return "hello, world! This com from spring!"; } } |
總結一下:
1 兩個maven依賴,spring-context;spring-mvc。maven就會自動下載所有關聯的jar包,包括
- spring-webmvc
- spring-beans
- spring-core
- spring-expression
- spring-web
- spring-context
- spring-aop
- aopalliance
- commons-logging
2 一個web.xml文件,配置了listener和servlet
3 兩個spring相關的文件,applicationContext.xml和servletName-servlet.xml
4 一個controller文件,配置了攔截的url處理代碼
有了這些準備工作,運行后輸入:http://localhost:8080/SpringTest/hello
就能得到
hello, world! This com from spring!
這樣的信息,恭喜你的SpringMVC搭起來了!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/xing901022/p/5240044.html