javascript
Spring MVC 使用介绍(二)—— DispatcherServlet
一、Hello World示例
1、引入依賴
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope> </dependency> <dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.2.5.RELEASE</version> </dependency>2、web.xml中定義servlet
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name>Archetype Created Web Application</display-name><servlet> <servlet-name>test-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/test-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup> </servlet><servlet-mapping> <servlet-name>test-servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
</web-app>
3、配置servlet
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"><!-- HandlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 處理器 --> <bean name="/hello" class="cn.matt.controller.TestController"/> </beans>配置說明:
- BeanNameUrlHandlerMapping:表示將請求的URL映射為Bean名,如URL為 “上下文/hello”,則Spring配置文件必須有一個名字為“/hello”的Bean
- SimpleControllerHandlerAdapter:表示所有實現了org.springframework.web.servlet.mvc.Controller接口的Bean可以作為Spring Web MVC中的處理器
- InternalResourceViewResolver:用于支持Servlet、JSP視圖解析
- viewClass:JstlView表示JSP模板頁面需要使用JSTL標簽庫,classpath中必須包含jstl的相關jar包
- prefix、suffix:查找視圖頁面的前綴和后綴(前綴[邏輯視圖名]后綴),比如傳進來的邏輯視圖名為hello,則該jsp視圖頁面應該存放在“WEB-INF/jsp/hello.jsp”
4、處理器
public class TestController implements Controller {public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {//1、收集參數、驗證參數 //2、綁定參數到命令對象 //3、將命令對象傳入業務對象進行業務處理 //4、選擇下一個頁面 ModelAndView mv = new ModelAndView(); //添加模型數據 可以是任意的POJO對象 mv.addObject("message", "Hello World!"); //設置邏輯視圖名,視圖解析器會根據該名字解析到具體的視圖頁面 mv.setViewName("hello"); return mv; } }5、視圖hello.jsp
<%@ page language="java" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body>${message} </body> </html>使用tomcat啟動,在瀏覽器輸入?http://localhost:8080/myweb/hello?即可訪問
?
二、DispatcherServlet詳解
1、DispatcherServlet在web.xml中的配置
<servlet> <servlet-name>test-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/test-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>test-servlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>load-on-startup:表示啟動容器時初始化該Servlet
url-pattern:表示哪些請求交給Spring Web MVC處理,?“/”?是用來定義默認servlet映射的,詳細可參考javaweb學習總結(五)——Servlet開發(一)
DispatcherServlet初始化配置:
- contextClass:實現WebApplicationContext接口的類, 默認使用XmlWebApplicationContext
- contextConfigLocation:指定上下文配置文件,可以被分成多個字符串(使用逗號作為分隔符) 來支持多個上下文,默認是“/WEB-INF/[servlet名字]-servlet.xml”。支持相對路徑和類路徑,如‘classpath:spring-mvc.xml’或‘/WEB-INF/test-servlet.xml’
- namespace:WebApplicationContext命名空間,默認值是[server-name]-servlet
2、通用上下文配置
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>通用上下文用于加載除Web層的其他Bean,如service、dao等,以便與其他Web框架集成
ContextLoaderListener初始化配置:
- contextConfigLocation:指定通用上下文配置文件,使用方式與DispatcherServlet相同,默認會去 /WEB-INFO/ 下加載applicationContext.xml
- contextClass:指定ApplicationContext的實現類,默認為XmlWebApplicationContext
3、DispatcherServlet上下文與通用上下文的關系
? ? ? ? ? ? ? ? ? ? ? ? ? ?
通用上下文:用于加載除Web層的其他Bean,如service、dao等,以便與其他Web框架集成;創建完畢后會將該上下文放在ServletContext:
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);DispatcherServlet上下文:用于加載只對Spring Web MVC有效的Bean,如Controller、HandlerMapping、HandlerAdapter等;創建時以通用上下文為父上下文,創建完畢后將該上下文放在ServletContext:
childContext.setParent(rootContext); servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT" + getServletName(), this.context);4、spring mvc的典型配置
<!-- spring-context.xml中的配置 --> <context:component-scan base-package="com.wind.pac" use-default-filters="true"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan><!-- spring-mvc.xml中的配置 --> <context:component-scan base-package="com.wind.pac" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /><context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" /> </context:component-scan>5、DispatcherServlet默認配置
DispatcherServlet的默認配置在DispatcherServlet.properties(和DispatcherServlet類在一個包下)中,是當Spring配置文件中沒有指定配置時使用的默認策略
?
注:eclipse下,web項目的運行目錄為:workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\
?
參考:
第二章 Spring MVC入門 —— 跟開濤學SpringMVC
第三章 DispatcherServlet詳解 ——跟開濤學SpringMVC
javaweb學習總結(五)——Servlet開發(一)
?
轉載于:https://www.cnblogs.com/MattCheng/p/9156057.html
總結
以上是生活随笔為你收集整理的Spring MVC 使用介绍(二)—— DispatcherServlet的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android超精准计步器开发-Dyla
- 下一篇: gradle idea java ssm