例子来自spring-framework-2.5.6.SEC01\docs\MVC-step-by-step\pdf\spring-mvc-step-by-step.pdf。
先上图,再慢慢解释。

1. DispatchServlet接过浏览器的/hello.htm请求
在springapp/war/WEB-INF/web.xml中,定义了homepage:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
所以浏览器端输入http://localhost:8080/springapp即相当于http://localhost:8080/springapp/index.jsp。
而springapp/war/index.jsp是直接sendRedirect:
<%-- Redirected because we can't set the welcome page to a virtual URL. --%>
<c:redirect url="/hello.htm"/>
所以又转为http://localhost:8080/springapp/hello.htm
在springapp/war/WEB-INF/web.xml中有:
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
所以这个http://localhost:8080/springapp/hello.htm请求由DispatcherServlet来处理。
2. DispatchServlet确定mapping of <Request, Controller>
DispatchServlet默认使用的是BeanNameUrlHandlerMapping,即通过controller的bean name来与request对应。
Controller在WebApplicationContext文件中定义,而WebApplicationContext文件的命名规则是<servlet-name>-servlet.xml,所以本案例的WebApplicationContext文件即是springapp-servlet.xml。
bean name必须是slash开头,然后只能写request中最后一个slash后面的部分。
<!-- spingapp/war/WEB-INF/springapp-servlet.xml -->
<bean name="/hello.htm" class="springapp.web.InventoryController">
<property ......></property>
</bean>
另外还有SimpleUrlHandlerMapping、ControllerClassNameHandlerMapping和CommonsPathMapHandlerMapping三种mapping形式,具体参见Spring in Action 2nd Edition 13.2
3. Controller返回ModelAndView给DispatchServlet
Controller的类型有很多,最简单的形式就是自己实现一个Controller接口(spring自带了很多Controller及其子接口的实现),只需实现一个handleRequest(HttpServletRequest, HttpServletResponse)方法,返回一个ModelAndView即可。
ModelAndView(ViewName, ModelName, ModelObject),其中的ModelName和ModelObject相当于给ViewName setAttribute(ModelName, ModelObject);ViewName可以写一个长路径,如“WEB-INF/jsp/hello.jsp”,更常见的方法是返回一个短字符串交给ViewResolver去解析。
4. DispatchServlet通过ViewResolver来解析ViewName
DispatchServlet默认使用的ViewResolver是InternalResourceViewResolver(更多关于DispatchServlet的默认配置请参见spring-framework-2.5.6.SEC01\src\org\springframework\web\servlet\DispatcherServlet.properties),但与BeanNameUrlHandlerMapping不同的是,虽然InternalResourceViewResolver是默认的,但需要进一步对InternalResourceViewResolver进行配置。
<!-- spingapp/war/WEB-INF/springapp-servlet.xml -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
prefix + “hello” + suffix == "/WEB-INF/jsp/hello.jsp"(这里的“解析”更像是“拼接”)
注意这里的bean id,虽然在spring-framework-2.5.6.SEC01\src\org\springframework\web\servlet\DispatcherServlet.java里有:
public static final String VIEW_RESOLVER_BEAN_NAME = "viewResolver";
但这里的bean id可以随便写。
However, MessageSource类的bean id必须是"messageSource",不能随便写。我们在spring-framework-2.5.6.SEC01\src\org\springframework\context\support\AbstractApplicationContext.java里面可以看到有:
public static final String MESSAGE_SOURCE_BEAN_NAME = "messageSource";
另外,bean name和bean id的区别:
Either one would work. It depends on your needs:
If your bean identifier contains special character(s) for example (/viewSummary.html), it (the slash) won't be allowed as the bean id, because it's not a valid XML ID. In such cases you could skip defining the bean id and supply the bean name instead.
The name attribute also helps in defining aliases for your bean, since it allows specifying multiple identifiers for a given bean.
5. JSP dispatched to browser by DispatchServlet
hello.jsp被dispatch给浏览器显示,注意是dispatch,所以浏览器的地址栏仍然是hello.htm。
JSP可以使用ModelAndView中的Model,形式如"${ModelName.ModelObject}",类似于getAttribute。
<%-- springapp/war/WEB-INF/jsp/hello.jsp --%>
<body>
<h1><fmt:message key="heading"/></h1>
<p><fmt:message key="greeting"/> <c:out value="${model.now}"></c:out></p>
<h3>Product</h3>
<c:forEach items="${model.products}" var="prod">
<c:out value="${prod.description}"/>
<i><c:out value="${prod.price}"/></i><br><br>
</c:forEach>
<br>
<a href="<c:url value="priceincrease.htm"/>">Increase Price</a>
<br>
</body>

- 大小: 33.4 KB
分享到:
相关推荐
### 游戏引擎剖析——Game Engine Anatomy 101 中文版 #### 一、游戏引擎概述 **游戏引擎**的概念起源于经典游戏《Doom》,这款游戏不仅因其出色的游戏体验而闻名,更在于它引领了一种全新的游戏开发模式——游戏...
- **Anatomy of a command(命令结构)**: 分析了一个典型Unix命令的组成部分。 - **Long options(长选项)**: 介绍了如何使用长选项来传递参数给命令。 #### 手册页阅读 - **Reading the fine manual(阅读手册...
The anatomy of a large-scale hypertextual web search engine[J]. Computer Networks and ISDN Systems, 1998, 30(1-7): 107-117. - [2] Robertson S E. Understanding inverse document frequency: on ...
e-anatomy.apk
Linux系统原理性书籍,共享内存是进程间通讯(IPC)的一种机制。本书深入地剖析了这种机制的原理,举例说明了使用方法。
Fundamentals of Anatomy and Physiology for Nursing and Healthcare Students is a succinct but complete overview of the structure and function of the human body, with clinical applications throughout....
Describes and illustrates the important aspects of anatomy, physiology, and pathophysiology.Clearly organized and written, this guide is ideal for students with a limited background in science....
学习人体的资源,密码xyfaka
根据《The Anatomy of a Large-Scale Hypertextual Web Search Engine》这篇经典论文,Google搜索引擎的目标是为用户提供一个能够覆盖广泛主题且准确度高的搜索服务。 #### 二、Google搜索引擎的技术挑战与创新 ##...
- **Anatomy of a Style**:详细解析了一个CSS样式的组成部分,包括选择器、属性和值。 - **Creating Styles and Style Sheets**:介绍了如何编写和组织CSS规则,以及如何使用不同的方法将样式应用到HTML文档中。 ...
- **Basic Asio Anatomy基本结构**:介绍了asio的基本组成部分,包括其架构设计与关键组件。 - **The Proactor Design Pattern: Concurrency Without Threads**:讨论了proactor模式及其如何在不使用线程的情况下...
http2-dump-anatomy 命令行工具,用于解码流量转储并将HTTP流量与相应的TCP数据包相关联。分析仪usage: http_traffic_analyzer.py [-h] [-k KEY] [-a] [-b BIN] [-o OUTPUT] [-g] [-s] [-q] [-v] dumpfileHTTP(2) ...
The study of human anatomy really comes to life in the anatomy laboratory. Here is where students get hands-on experience with human cadavers and bones, classroom models, preserved and fresh animal ...
-Radiology With a special emphasis on the complementary nature of anatomic and functional cardiac data, Clinical Cardiac CT: Anatomy and Function -- now in a lavishly illustrated Second Edition -- ...
ALPHA Camp 2021学期一,三月班履历网页作业:内容解析总结记录在2021年ALPHA Camp学期一,三月班第三周履历网页作业使用到的技术,思路与开发流程。版本与环境Google Chrome: 89.0.4389.90 (Official Build) (64-...
### 计算机类英语词汇解析 #### 1. Counterfeit (伪造的) - **定义**: 表示伪造或仿造的东西,常用于指非法复制的产品。 - **应用**: 在网络安全与版权保护方面,用于讨论如何识别并防止假冒软件、硬件及数字内容。...
文章《The Anatomy of a Large-Scale Hypertextual Web Search Engine》(以下简称《Anatomy》)深入介绍了Google搜索引擎的设计理念和技术细节,这是一款能够高效抓取网页并建立索引的大型搜索引擎。 #### 二、...
- **Anatomy Demystified**:涵盖了人体解剖学的基本知识。 - **Astronomy Demystified**:介绍了天文学的基本概念和技术。 - **Biology Demystified**:提供了生物学领域的基础知识。 - **Business Statistics ...
e-Anatomy.apk