1.
SpringMVC框架認識
Spring MVC是一個基于MVC模式的Web框架,SpringMVC作為Spring中的一個模塊,它與Spring能夠無縫集成,主要用于解決企業Web開發中常見的問題:如參數接收、文件上傳、表單驗證、國際化等等。
2.
SpringMVC HelloWorld模式使用
2.1.
導入Spring與SpringMVC的依賴jar包
①
配置Spring的環境
準備Spring的環境——SpringMVC以Spring為核心,而Spring最核心的模塊是DI/IOC容器,也就是SpringMVC的核心類和控制器要交給Spring管理。所以使用SpringMVC必須先保證Spring容器初始化,也就是讓項目擁有Spring的運行環境。
基于Spring的初始化環境需要導入Spring的核心包與beans包(使用spring-framework-4.1.2.RELEASE),jar包目錄位置在spring-framework-4.1.2.RELEASElibs文件中:
需要導入:
com.springsource.org.apache.commons.logging-1.1.1.jar
spring-beans-4.1.2.RELEASE.jar
spring-context-4.1.2.RELEASE.jar
spring-core-4.1.2.RELEASE.jar
spring-expression-4.1.2.RELEASE.jar
配置applicationContext.xml,具體配置參照如下:
http://www.springframework.org/schema/beans/spring-beans.xsd">
創建Junit4測試類查看是否獲取到配置的Bean對象:
importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;public classSpringEnvTest {
@org.junit.Testpublic voidGetBeanTest(){//讀取資源文件applicationContext.xml//拿到ApplicationContext核心對象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("-------------實例化分割線--------------------");//獲取Bean同樣具有三種方式:這里不再一一列出
Object myDate = applicationContext.getBean("myDate");
System.out.println(myDate);
}
}
②配置SpringMVC的環境
SpringMVC作為Spring中的一個模塊存在,無需單獨下載,Spring的lib文件中就包含了SpringMVC的jar包。
jar包目錄位置也是在spring-framework-4.1.2.RELEASElibs中,需要導入的jar包:
spring-web-4.1.2.RELEASE.jar
spring-webmvc-4.1.2.RELEASE.jar
2.2.在web.xml中配置核心控制器DispatcherServlet
在SpringMvc框架中使用的是DispatcherServlet作為核心控制器,DispatcherServlet核心控制器會攔截匹配的請求,把攔截下來的請求,依據相應的規則分發到目標Controller來處理。
所以,我們要在web.xml中配置SpringMVC的核心控制器DispatcherServlet,具體配置如下:
web.xml配置:
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext-mvc.xml
1
dispatcherServlet
/
2.2.配置SpringMVC的配置文件
SpringMVC的配置文件和Spring的配置差不多,在resources目錄中新建一個applicationContext-mvc.xml文件即可,需要配置映射Controller類的映射路徑及class:
http://www.springframework.org/schema/beans/spring-beans.xsd">
2.3.準備Controller控制器并配置
這里需要創建一個HelloController類來實現Controller接口,實現handleRequest()方法:
importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;public class HelloController implementsController {/***
* 控制層業務處理:①參數的接收;②調用Service層的業務邏輯;③返回一個視圖
*@paramequest
*@paramresponse
*@return*@throwsException*/@Overridepublic ModelAndView handleRequest(HttpServletRequest equest, HttpServletResponse response) throwsException {
System.out.println("進入Springmvc Controller視圖模型控制器......");
String view= "/WEB-INF/views/hello.jsp"; //視圖
String modelName = "msg"; //模型名字
Object modelObject = "helloObj"; //模型對象
ModelAndView modelAndView = newModelAndView(view, modelName, modelObject);returnmodelAndView;
}
}
2.4.根據applicationContext-mvc.xml訪問映射路徑
運行Tomcat并啟動web項目,訪問配置的url路徑,即可訪問到對應的/hello映射的/WEB-INF/views/hello.jsp文件。
3.?? SpringMVC入門Demo的執行流程分析
從上面的使用配置過程我們可以分析得出SpringMVC的執行流程:
首先,我們通過瀏覽器客戶端發起請求(localhost:8080/hello),這個請求會被web.xml中配置的核心控制器DispatcherServlet所攔截,攔截到的請求地址會交由配置在classpath下的applicationContext-mvc.xml SpringMVC的配置文件中去找到對應的bean的請求url,并且找到對應的Bean class,通過Bean class配置再找到對應的Controller,通過Controller中的handleRequest()方法返回的對應的ModelAndView視圖,返回對應的hello.jsp頁面,通過頁面hello.jsp去呈現頁面上的內容。
4.?? 靜態資源文件訪問放行
通過上面的配置,我們如果在Webapp路徑下存在靜態資源(.html與css/js)文件時,我們通過瀏覽器直接去訪問是訪問不了的,會出現404 not find問題。
原因就在于我們在web.xml中默認配置了攔截所有的請求,我們需要在SpringMVC的配置文件applicationContext-mvc.xml中去開啟對靜態資源的訪問:
applicationContext-mvc.xml具體配置如下:
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
配置之后,通過瀏覽器頁面就能訪問到webapp路徑下的靜態資源文件了。