整体过程
- 发起请求到前端控制器(DispatcherServlet)
- 前端控制器请求HandlerMapping查找Handler
可以根据xml配置, 注解进行查找 - 处理器映射器HandlerMapping 向前端控制器返回Handler
- 前端控制器调用处理器适配handleAdapter器去执行Handler
- 处理器适配器去执行handler
- handler执行完成给适配器返回modelandview
- 处理器适配器向前端控制器返回modelAndview
modelandview是springmvc框架的 一个底层对象,包括modelandview - 前端控制器请求视图解析器去进行视图解析
- 视图解析器向前端控制器返回view
- 前端控制器进行视图渲染
视图渲染将Modelandview中的数据填充到response域
11.前端控制器向用户响应结果
基于非注解的配置
- 使用
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
要注意对应的handler需要分别实现对应的接口才会被适配器所执行
public class HttpRequestHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return (handler instanceof HttpRequestHandler);
}
}
public class SimpleControllerHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return (handler instanceof Controller);
}
}
这两种也是dispatcher的默认配置,所以不需要在dispatcher-servlet.xml中显式配置也可以。默认配置文件可以在对应jar包(org.springframework.web.servlet)中获取
# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.
org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager
基于注解的方式配置
配置annotationdispatcher-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
<!--mvc:annotation-driven可以用来代替以上RequestMappingHandlerMapping和RequestMappingHandlerAdapter 的关系-->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.vee.controller"></context:component-scan>
<!--视图解析器,解析jsp,默认使用jstl标签-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置前缀和后缀就可以在写modleAndView的viewName的时候不用写出完整的绝对路径名-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
同时web.xml中servlet也要修改成annotationdispatcher
<servlet>
<servlet-name>annotationdispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>annotationdispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
使用注解的Controller类
//使用注解标识的控制器
@Controller
public class AnnotationHandler {
@RequestMapping("/wocaolei")
public ModelAndView handle(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("mesg", "annotationHandler..");
//这个是没有使用前后缀的完整路径
modelAndView.setViewName("/WEB-INF/jsp/welcome.jsp");
return modelAndView;
}
//如果返回结果是字符串adapter会把他当成modelAndView中的viewName 来处理
@RequestMapping("/wocaolei2")
public String handle2(){
System.out.println("annotation handle2");
return "welcome";
}
}