spring mvc本质上还是在使用Servlet处理,并在其基础上进行了封装简化了开发流程,提高易用性、并使用程序逻辑结构变得更清晰
spring-study
com.myx
1.0-SNAPSHOT
4.0.0
jar
spring-mvc
org.springframework
spring-webmvc
${spring.version}
javax.servlet
javax.servlet-api
3.1.0
javax.servlet
jstl
1.2
taglibs
standard
1.1.2
org.mortbay.jetty
jetty-maven-plugin
7.4.5.v20110725
/
3
src/main/webapp
**/*.xml
8081
400000
spring-mvc
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:/spring-mvc.xml
dispatcherServlet
/
/index.html
simpleController
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 描述:简单路由映射实现mvc
* @author: myx
* @date: 2019/1/14
* Copyright © 2018-hotpot. All rights reserved.
*/
public class SimpleController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView("userView");
mv.addObject("name", "myx is good man");
return mv;
}
}
其为mvc 中url路径与Control对像的映射,DispatcherServlet 就是基于此组件来寻找对应的Control,如果找不到就会报Not Found mapping 的异常。
目前主流的三种mapping:
import org.springframework.web.HttpRequestHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 描述:BeanNameUrlHandlerMapping 测试
* @author: myx
* @date: 2019/1/14
* Copyright © 2018-hotpot. All rights reserved.
*/
public class BeanNameController implements HttpRequestHandler {
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("myx is good man");
}
}
这里spring mvc 采用适配器模式来适配调用指定Handler,根据Handler的不同种类采用不同的Adapter,其Handler与 HandlerAdapter 对应关系如下:
Handler类别 | 对应适配器 | 描述 |
---|---|---|
Controller | SimpleControllerHandlerAdapter | 标准控制器,返回ModelAndView |
HttpRequestHandler | HttpRequestHandlerAdapter | 业务自行处理 请求,不需要通过modelAndView 转到视图 |
Servlet | SimpleServletHandlerAdapter | 基于标准的servlet 处理 |
HandlerMethod | RequestMappingHandlerAdapter | 基于@requestMapping对应方法处理 |
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().println("hello luban ");
}
}
找到应的Adapter 之后就会基于适配器调用业务处理,处理完之后业务方会返回一个ModelAndView ,在去查找对应的视图进行处理。其在org.springframework.web.servlet.DispatcherServlet#resolveViewName() 中遍历 viewResolvers 列表查找,如果找不到就会报一个 Could not resolve view with name 异常。
在下一步就是基于ViewResolver.resolveViewName() 获取对应View来解析生成Html并返回 。对应VIEW结构如下:
至此整个正向流程就已经走完了,如果此时程序处理异常 MVC 该如何处理呢?
该组件用于指示 当出现异常时 mvc 该如何处理。 dispatcherServlet 会调用org.springframework.web.servlet.DispatcherServlet#processHandlerException() 方法,遍历 handlerExceptionResolvers 处理异常,处理完成之后返回errorView 跳转到异常视图。
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 描述:异常
* @author: myx
* @date: 2019/1/14
* Copyright © 2018-hotpot. All rights reserved.
*/
public class SimpleExceptionHandle implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
return new ModelAndView("error");
}
}
除了上述组件之外 spring 中还引入了 我Interceptor 拦截器 机制,类似于Filter。
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 描述:拦截器
* @author: myx
* @date: 2019/1/14
* Copyright © 2018-hotpot. All rights reserved.
*/
public class SimpleHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
其实现机制是基于 HandlerExecutionChain 分别在 doDispatch 方法中执行以下方法:
具体逻辑源码参见:org.springframework.web.servlet.DispatcherServlet#doDispatch 方法。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* 描述:注解实现
* @author: myx
* @date: 2019/1/14
* Copyright © 2018-hotpot. All rights reserved.
*/
@Controller
public class SimpleController{
/**
* 注解方法
* @return
*/
@RequestMapping("/hello.do")
public ModelAndView hello() {
ModelAndView mv = new ModelAndView("userView");
mv.addObject("name", "myx 注解");
return mv;
}
}
只要查看以类的源就可以知晓其中原因:
结论:
在