javascript
spring— Spring与Web环境集成
ApplicationContext應用上下文獲取方式
應用上下文對象是通過new ClasspathXmlApplicationContext(spring配置文件) 方式獲取的,但是每次從容器中獲 得Bean時都要編寫new ClasspathXmlApplicationContext(spring配置文件) ,這樣的弊端是配置文件加載多次, 應用上下文對象創建多次。
在Web項目中,可以使用ServletContextListener監聽Web應用的啟動,我們可以在Web應用啟動時,就加載 Spring的配置文件,創建應用上下文對象ApplicationContext,在將其存儲到最大的域servletContext域中,這樣 就可以在任意位置從域中獲得應用上下文ApplicationContext對象了。
Spring提供獲取應用上下文的工具
Spring提供了一個監聽器ContextLoaderListener就是對上述功能的封裝,該監聽器內 部加載Spring配置文件,創建應用上下文對象,并存儲到ServletContext域中,提供了一個客戶端工具 WebApplicationContextUtils供使用者獲得應用上下文對象。
所以我們需要做的只有兩件事:
①在web.xml中配置ContextLoaderListener監聽器(導入spring-web坐標)
②使用WebApplicationContextUtils獲得應用上下文對象ApplicationContext
SpringMVC開發步驟
①導入SpringMVC相關坐標
<!--SpringMVC坐標--><dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId><version>5.0.5.RELEASE</version></dependency> <!--Servlet坐標--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency><!--Jsp坐標--><dependency> <groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId> <version>2.0</version></dependency>②配置SpringMVC核心控制器DispathcerServlet
<?xml version="1.0" encoding="UTF-8"?> <web-app 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_4_0.xsd"version="4.0"> <!-- <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>--><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app>③創建Controller類和視圖頁面
④使用注解配置Controller類中業務方法的映射地址
⑤配置SpringMVC核心文件 spring-mvc.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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "><context:component-scan base-package="com.controller" /><mvc:annotation-driven /> </beans>⑥客戶端發起請求測試
SpringMVC的執行流程
①用戶發送請求至前端控制器DispatcherServlet。 ②DispatcherServlet收到請求調用HandlerMapping處理器映射器。
③處理器映射器找到具體的處理器(可以根據xml配置、注解進行查找),生成處理器對象及處理器攔截器(如果有則 生成)一并返回給DispatcherServlet。
④DispatcherServlet調用HandlerAdapter處理器適配器。
⑤HandlerAdapter經過適配調用具體的處理器(Controller,也叫后端控制器)。
⑥Controller執行完成返回ModelAndView。
⑦HandlerAdapter將controller執行結果ModelAndView返回給DispatcherServlet。
⑧DispatcherServlet將ModelAndView傳給ViewReslover視圖解析器。
⑨ViewReslover解析后返回具體View。
⑩DispatcherServlet根據View進行渲染視圖(即將模型數據填充至視圖中)。DispatcherServlet響應用戶
SpringMVC組件解析
用戶請求到達前端控制器,它就相當于 MVC 模式中的 C,DispatcherServlet 是整個流程控制的中心,由它調用其它組件處理用戶的請求,DispatcherServlet 的存在降低了組件之間的耦合性。
HandlerMapping 負責根據用戶請求找到 Handler 即處理器,SpringMVC 提供了不同的映射器實現不同的映射方式,例如:配置文件方式,實現接口方式,注解方式等。
通過 HandlerAdapter 對處理器進行執行,這是適配器模式的應用,通過擴展適配器可以對更多類型的處理器進行執行。
它就是我們開發中要編寫的具體業務控制器。由 DispatcherServlet 把用戶請求轉發到 Handler。由 Handler 對具體的用戶請求進行處理。
負責將處理結果生成 View 視圖,View Resolver 首先根據邏輯視圖名解析成物理視圖名,即具體 的頁面地址,再生成 View 視圖對象,最后對 View 進行渲染將處理結果通過頁面展示給用戶。
SpringMVC 框架提供了很多的 View 視圖類型的支持,包括:jstlView、freemarkerView、pdfView等。最常用的 視圖就是 jsp。一般情況下需要通過頁面標簽或頁面模版技術將模型數據通過頁面展示給用戶,需要由程序員根據 業務需求開發具體的頁面
3.3 SpringMVC注解解析
@RequestMapping
作用:用于建立請求 URL 和處理請求方法之間的對應關系
位置:
? 類上,請求URL 的第一級訪問目錄。此處不寫的話,就相當于應用的根目錄
? 方法上,請求 URL 的第二級訪問目錄,與類上的使用@ReqquestMapping標注的一級目錄一起組成訪問虛擬路徑
屬性:
? value:用于指定請求的URL。它和path屬性的作用是一樣的
? method:用于指定請求的方式
? params:用于指定限制請求參數的條件。它支持簡單的表達式。要求請求參數的key和value必須和配置的一模一樣
例如:
? params = {“accountName”},表示請求參數必須有accountName
? params = {“moeny!100”},表示請求參數中money不能是10
@Controller("controller") public class QuickController {public QuickController() {System.out.println("aa");}@RequestMapping(value = {"/qq"},method = {RequestMethod.GET},params = {"name"})public String method(){System.out.println("controller");return "success.jsp";} }3.4 SpringMVC的XML配置解析
SpringMVC有默認組件配置,默認組件都是DispatcherServlet.properties配置文件中配置的,該配置文件地址org/springframework/web/servlet/DispatcherServlet.properties,該文件中配置了默認的視圖解析器,如下:
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver翻看該解析器源碼,可以看到該解析器的默認設置,如下:
REDIRECT_URL_PREFIX = "redirect:" --重定向前綴 FORWARD_URL_PREFIX = "forward:" --轉發前綴(默認值) prefix = ""; --視圖名稱前綴 suffix = ""; --視圖名稱后綴我們可以通過屬性注入的方式修改視圖的的前后綴
<!--配置內部資源視圖解析器--> <?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "><context:component-scan base-package="com.controller" /><mvc:annotation-driven /><bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="suffix" value=".jsp"/><property name="prefix" value="/WEB-INF/"/></bean> </beans>總結
以上是生活随笔為你收集整理的spring— Spring与Web环境集成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode 1423. 可获得的最
- 下一篇: spring—SpringMVC的请求和