手把手教你搭建SpringMVC框架——最小化配置
為什么需要Spring MVC
最開始接觸網(wǎng)頁的時候,是純的html/css頁面,那個時候還是用Dreamweaver來繪制頁面。
隨著網(wǎng)站開發(fā)的深入,開始學(xué)習(xí)servlet開發(fā),記得最痛苦的就是servlet返回網(wǎng)頁的內(nèi)容是字符串拼接的html頁面,整不好就無法顯示....
再到后來開學(xué)學(xué)習(xí)SSH,龐大的架構(gòu)眼花繚亂。Struts繁雜的標(biāo)簽、hibernate搞不清楚的數(shù)據(jù)表,Spring不知道哪里搞錯的bean。
最后隨著發(fā)展,前端開始占有一席之地,nodejs風(fēng)生水起,很多業(yè)務(wù)邏輯開始前置。再也看不到當(dāng)初的bo、dao了,取而代之的是各種框架的mvvm,后臺減輕壓力只負(fù)責(zé)一些必要的邏輯。
到現(xiàn)在,好像web開發(fā)又發(fā)展到了一個階段——前端由于Nodejs的作用,可以支撐一部分業(yè)務(wù)邏輯,通過轉(zhuǎn)發(fā)代理,統(tǒng)一發(fā)往后臺。后臺通過url實現(xiàn)mvc,對性持久化、更深入的邏輯操作等等。Spring MVC在這里就起了很關(guān)鍵的作用....它通過Url攔截請求,自定義業(yè)務(wù)邏輯,可以返回自定義的view或者模型數(shù)據(jù)。
當(dāng)然,上面的鬼扯都是片面的,不代表行業(yè)的發(fā)展,只是博主管中窺豹而已。
下面步入正題,說說Spring MVC的最小化配置,給入門的朋友引個路。
Spring MVC的最小化配置
需要的jar包
- Spring framework spring-context
- Spring framework spring-mvc
具體可以參考maven中的引用:
<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配置
<?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> <!-- 默認(rèn)是/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> <!-- 默認(rèn)是/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的相關(guān)內(nèi)容,并配置攔截的url,如上面所示,所有/開頭的請求,都會通過SpringMVC這個servlet進(jìn)行處理。
他們都需要一個xml文件,默認(rèn)位置上面已經(jīng)說過了。
applicationContext.xml
空的,反正咱也沒用什么bean。
<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的配置即可。
<?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"> <!-- 設(shè)置使用注解的類所在的jar包 --> <context:component-scan base-package="hello" /> </beans>
controller文件
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!"; } }
總結(jié)一下:
1 兩個maven依賴,spring-context;spring-mvc。maven就會自動下載所有關(guān)聯(lián)的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相關(guān)的文件,applicationContext.xml和servletName-servlet.xml
4 一個controller文件,配置了攔截的url處理代碼
有了這些準(zhǔn)備工作,運(yùn)行后輸入:http://localhost:8080/SpringTest/hello
就能得到
hello, world! This com from spring!
這樣的信息,恭喜你的SpringMVC搭起來了!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Spring框架之基于Restful風(fēng)格實現(xiàn)的SpringMVC
- 詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實用
- 詳解SpringMVC驗證框架Validation特殊用法
- 在SpringMVC框架下實現(xiàn)文件的上傳和下載示例
- Java框架篇:Spring+SpringMVC+hibernate整合開發(fā)
- springMVC框架下JQuery傳遞并解析Json數(shù)據(jù)
- JavaWeb開發(fā)之Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基礎(chǔ)框架
- jquery.form.js框架實現(xiàn)文件上傳功能案例解析(springmvc)
- 使用jQuery.form.js/springmvc框架實現(xiàn)文件上傳功能
- 三步輕松搭建springMVC框架
相關(guān)文章
Spring Boot + Mybatis-Plus實現(xiàn)多數(shù)據(jù)源的方法
這篇文章主要介紹了Spring Boot + Mybatis-Plus實現(xiàn)多數(shù)據(jù)源的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11基于Spring上下文工具類?ApplicationContextUtil
這篇文章主要介紹了基于Spring上下文工具類?ApplicationContextUtil,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11springboot項目使用Disruptor做內(nèi)部消息隊列的實現(xiàn)
本文主要介紹了springboot項目使用Disruptor做內(nèi)部消息隊列的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07spring security CSRF防護(hù)的示例代碼
這篇文章主要介紹了spring security CSRF防護(hù)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03