通过上一篇springmvc的原理学习后。
不同的处理器适配器(HandlerAdapter)执行实现不同规则接口的处理器(Handler),所以开发Handler对应于HandlerAtapter
1. SimpleControllerHandlerAdapter(处理器适配器)执行实现Controller接口处理器(Handler)
DemoController 实现
public class DemoController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("我是dispatch1下handler");
// 调用service层处理数据,为简单demo,这里使用静态数据
List<User> list = new ArrayList<User>();
list.add(new User((long) 1,"zhangsan"));
list.add(new User((long) 2,"lisi"));
//创建方法返回对象
ModelAndView modelview = new ModelAndView();
//查看ModelAndView源码发现一个对象含有modelMap,view,HttpStatus,cleared属性
// 将数据存储到modelMap ,该方法相当于request.setAttribute();
modelview.addObject("list", list);
//指定视图
modelview.setViewName("/WEB-INF/jsp/demo/demo.jsp");
return modelview;
}
}
2. HttpRequestHandlerAdapter(处理器适配器)执行实现HttpRequestHandler接口的处理器(Handler)
DemoHttpRequestHandler 实现
public class DemoHttpRequestHandler implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我是dispatch1下httprequesthandler");
// 调用service层处理数据,为简单demo,这里使用静态数据
List<User> list = new ArrayList<User>();
list.add(new User((long) 1,"zhangsan"));
list.add(new User((long) 2,"lisi"));
//存放数据
request.setAttribute("list", list);
//跳转页面
request.getRequestDispatcher("/WEB-INF/jsp/demo/demo.jsp").forward(request, response);
}
}
3 . 前端控制器DispatchServlet的注册(可以同时存在多个DispatcheServlet)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!--使用Spring MVC,配置DispatcherServlet是第一步。
DispatcherServlet是一个Servlet,,所以可以配置多个DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
contextConfiigLoaction配置springmvc加载的配置文件(配置处理器映射器,适配器 。。。)
如果不配置contextConfigLoaction参数value默认为WEB-INF下 项目名-serlvet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--
第一种:url以*.do 访问由此DispatchServlet解析处理 ,不能设置/*.do tomcat7/8.5报错 java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component
第二种:/ 所有访问地址都由这个DispatchServlet解析,对于静态文件需要配置不让DispatchServlet处理
使用这种方式可以实现Restful风格url
第三种:/* 这样最终转发到jsp页面,仍由DispathchServlet解析地址不能找到Handler,报错????
用/* 则@requesetMapping("xxx.html") -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>springmvc1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc1.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc1</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4. 其中一个DispatchServlet对应springmvc.xml
springmvc.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:p="http://www.springframework.org/schema/p"
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/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">
<!-- 配置处理器映射器(处理器映射器可以多个共存)
映射器选择BeanNameUrlHandlerMapping,将handlerbean的name作为url进行查找,需要Handler配置指定name为url
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 另一种处理器映射器 SimpleUrlHandlerMapping 根据key指定url value指定具体handlerbean的配置id-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/demo1.do">demoContorller</prop>
<!-- 可以配置多个 ... -->
<prop key="/demo2.do">httpRequestHandler</prop>
</props>
</property>
</bean>
<!-- 配置处理器适配器 (处理器适配器可以多个共存)
所有处理器适配器都实现于 HandlerAdapter接口 ,查看源码 适配器通过执行handler(request,response,handler)方法返回ModelAndView
,而方法中 通过参数handler执行handleRequest(request,response)得到ModelAndView
而每个handler都是Controller的实现类对象,所以我们可以开发Handler实现不同处理器
SimpleControllerHandlerAdapter 处理器适配器可以处理执行Controller接口的处理器类
-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 另一种处理器适配器HttpRequestHandlerAdapter ,不同适配器执行不同规则handler
该适配器执行实现HttpRequestHandler接口Handler,编写实现Handler由开发者控制
-->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
<!-- 配置处理器 (spring5.0 无需要求处理器配置于其他主键前)
处理可以开发 需要实现Contorller接口的Handler /demo.do 为该配置文件DispatchServlet 所对应的url-pattern
-->
<bean id="demoContorller" name="/demo.do" class="com.springmvcdemo.handlers.DemoController" />
<!-- 配置实现HttpRequestHandler接口的处理器 只被SimpleUrlHandlerMapping映射器映射只需设置id-->
<bean id="httpRequestHandler" class="com.springmvcdemo.handlers.DemoHttpRequestHandler"/>
<!-- 配置视图解析器
解析jsp,默认使用jstl标签,classpath下的所有jstl包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
</beans>
5. 未配置各大组件时时,springmvc会默认启动org.springframework.web/servlet.DispatcherServlet.properties下所有配置
DispatcherServlet.properties
# 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.method.annotation.RequestMappingHandlerMapping
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver,\
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