web.xml
<web-app>
<!--配置前端控制器-->
<servlet>
<servlet-name>HelloDavid</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置springmvc加载的配置文件,默认文件名为[servlet-name]-servlet.xml和默认位置为WebContent/WEB-INF,也可以自定义(此配置文件用来配置处理器映射器\适配器)-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--<servlet-mapping>标记指示哪些URL将由DispatcherServlet处理。-->
<servlet-mapping>
<servlet-name>HelloDavid</servlet-name>
<!--两种配置方式-->
<!-- *.form:所有访问.form结尾的,都由dispatcher进行解析-->
<!-- /:所有访问的地址都由dispatcher进行解析,对于静态文件的解析需要配置不让dispatcher进行解析,此种方式可以实现RESTful风格的url-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
dispatch-servlet.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" xmlns:p="http://www.springframework.org/schema/p"
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">
<!--处理器适配器-->
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!--处理器映射器(将bean的name当作url进行查找,需要在配置handler时指定beanname)-->
<bean name="/HelloWorld" class="controller.ItemsController1"></bean>
<!--视图解析器-->
<!--此视图解析器用来解析jsp-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀是路径,后缀是视图文件的后缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
注意web.xml中的<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
这一句意思是使用DispatcherServlet这个前端控制器,那么它到底干了什么呢?注意DispatcherServlet类的源码中的doDispatch方法
1.从request中获取handler的名字
mappedHandler = getHandler(processedRequest);
- getHandler()方法从request中取出handler,至于怎么取的,待研究
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
for (HandlerMapping hm : this.handlerMappings) {
if (logger.isTraceEnabled()) {
logger.trace(
"Testing handler map [" + hm + "] in DispatcherServlet with name '" + getServletName() + "'");
}
HandlerExecutionChain handler = hm.getHandler(request);
if (handler != null) {
return handler;
}
}
return null;
}
2.从mappedHandler中获取HandlerAdapter
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
-
getHandlerAdapter()源码,获取HandlerAdapter,怎么获取的?看初始化
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
for (HandlerAdapter ha : this.handlerAdapters) {
if (logger.isTraceEnabled()) {
logger.trace("Testing handler adapter [" + ha + "]");
}
if (ha.supports(handler)) {
return ha;
}
}
throw new ServletException("No adapter for handler [" + handler +
"]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
}
3.获取modelAndView
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
这时候,已经从mappedHandler中知道了Handler的名字,于是执行该Handler,返回ModelAndView
public class ItemsController1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
{
List<Item> itemList = new ArrayList<>();
Item item1 = new Item();
item1.setName("联想笔记本");
item1.setPrice(6000f);
item1.setDetail("thinkpad 超极本");
Item item2 = new Item();
item2.setName("苹果笔记本");
item2.setPrice(8000f);
item2.setDetail("ipad 超极本");
itemList.add(item1);
itemList.add(item2);
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute,在前端通过itemList取数据
modelAndView.addObject("itemList",itemList);
//指定视图
modelAndView.setViewName("hello");
return modelAndView;
}
}
这个ItemsController1类实现了Controller接口,并重写其中的handleRequest方法,返回ModelAndView。这里的ModelAndView类如下:
其中有两个主要的成员
public class ModelAndView {
/** View instance or view name String */
private Object view;
/** Model Map */
private ModelMap model;
其中ModelMap类,用来存放模型,继承了LinkedHashMap类,于是它的addAttribute方法,其实就是HashMap的put方法,用于加入key-value
view是Object类,用setViewName指定视图的名字