制作微信网站,wordpress手机登录跳转页面模板,wordpress 主题切换,网站报错 自动404转载自 Spring思维导图#xff0c;让Spring不再难懂#xff08;mvc篇#xff09;spring mvc简介与运行原理Spring的模型-视图-控制器#xff08;MVC#xff09;框架是围绕一个DispatcherServlet来设计的#xff0c;这个Servlet会把请求分发给各个处理器#xff0c;并支持…转载自 Spring思维导图让Spring不再难懂mvc篇
spring mvc简介与运行原理Spring的模型-视图-控制器MVC框架是围绕一个DispatcherServlet来设计的这个Servlet会把请求分发给各个处理器并支持可配置的处理器映射、视图渲染、本地化、时区与主题渲染等甚至还能支持文件上传。原理.png(1) Http请求客户端请求提交到DispatcherServlet。(2) 寻找处理器由DispatcherServlet控制器查询一个或多个HandlerMapping找到处理请求的Controller。(3) 调用处理器DispatcherServlet将请求提交到Controller。(4)(5)调用业务处理和返回结果Controller调用业务逻辑处理后返回ModelAndView。(6)(7)处理视图映射并返回模型 DispatcherServlet查询一个或多个ViewResoler视图解析器找到ModelAndView指定的视图。(8) Http响应视图负责将结果显示到客户端。主要注解spring mvc注解.pngContextLoaderListener在讲ContextLoaderListener之前首先来了解一下web.xml的作用。一个web中可以没有web.xml文件也就是说web.xml文件并不是web工程必须的。web.xml文件是用来初始化配置信息比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。当你的web工程没用到这些时你可以不用web.xml文件来配置你的Application。当要启动某个web项目时服务器软件或容器如tomcat会第一步加载项目中的web.xml文件通过其中的各种配置来启动项目只有其中配置的各项均无误时项目才能正确启动。web.xml有多项标签在其加载的过程中顺序依次为context-param listener fileter servlet。同类多个节点以出现顺序依次加载web.xml加载过程.png而spring mvc启动过程大致分为两个过程ContextLoaderListener初始化实例化IoC容器并将此容器实例注册到ServletContext中。DispatcherServlet初始化。web.xml配置.png其中ContextLoaderListener监听器它实现了ServletContextListener这个接口在web.xml配置这个监听器启动容器时就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类所以整个加载配置过程由ContextLoader来完成。ContextLoaderListener在web.xml中的配置!-- 配置contextConfigLocation初始化参数 --context-param param-namecontextConfigLocation/param-name param-value/WEB-INF/applicationContext.xml/param-value/context-param!-- 配置ContextLoaderListerner --listener listener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenerServletContextListener 接口有两个方法:contextInitialized,contextDestroyedDispatcherServletSpring MVC框架与其他很多web的MVC框架一样请求驱动所有设计都围绕着一个中央Servlet来展开它负责把所有请求分发到控制器同时提供其他web应用开发所需要的功能。不过Spring的中央处理器DispatcherServlet能做的比这更多。下图展示了Spring Web MVC的DispatcherServlet处理请求的工作流。熟悉设计模式的朋友会发现DispatcherServlet应用的其实就是一个“前端控制器”的设计模式其他很多优秀的web框架也都使用了这个设计模式。流程图spring mvc处理请求的流程.jpg在web.xml中的配置!-- servlet定义 --servlet servlet-namedispatcher/servlet-name servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class load-on-startup1/load-on-startup/servletservlet-mapping servlet-namedispatcher/servlet-name url-pattern//url-pattern/servlet-mapping其中load-on-startup表示启动容器时初始化该Servleturl-pattern表示哪些请求交给Spring Web MVC处理 “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。在Spring MVC中每个DispatcherServlet都持有一个自己的上下文对象WebApplicationContext它又继承了根rootWebApplicationContext对象中已经定义的所有bean。这些继承的bean可以在具体的Servlet实例中被重载在每个Servlet实例中你也可以定义其scope下的新bean。WebApplicationContext继承自ApplicationContext它提供了一些web应用经常需要用到的特性。它与普通的ApplicationContext不同的地方在于它支持主题的解析并且知道它关联到的是哪个servlet它持有一个该ServletContext的引用DispatcherServlet继承结构spring mvc同时提供了很多特殊的注解用于处理请求和渲染视图等。DispatcherServlet初始化的过程中会默认使用这些特殊bean进行配置。如果你想指定使用哪个特定的bean你可以在web应用上下文WebApplicationContext中简单地配置它们。特殊bean.png其中常用的ViewResolver的配置。以jsp作为视图为例!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --bean classorg.springframework.web.servlet.view.InternalResourceViewResolver property nameprefix value/WEB-INF/jsp/ / property namesuffix value.jsp //bean配置上传文件限制MultipartResolver!-- 上传限制 --bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver !-- 上传文件大小限制为31M31*1024*1024 -- property namemaxUploadSize value32505856//beanapplicationContext.xml中的标签applicationContext.xml配置文件标签.png文件上传前面说到DispatcherServlet中有个特殊的Bean叫MultipartResolver可用于限制文件的上传大小等。当解析器MultipartResolver完成处理时请求便会像其他请求一样被正常流程处理。表单form methodpost action/form enctypemultipart/form-datainput typetext namename/input typefile namefile/input typesubmit/
/form控制器RequestMapping(path /form, method RequestMethod.POST) public String handleFormUpload(RequestParam(name) String name, RequestParam(file) MultipartFile file) { if (!file.isEmpty()) { byte[] bytes file.getBytes(); // store the bytes somewhere return redirect:uploadSuccess; } return redirect:uploadFailure;}异常处理先来说下常见的异常处理有几种方式如下图异常处理方式.pngSpring的处理器异常解析器HandlerExceptionResolver接口的实现负责处理各类控制器执行过程中出现的异常。也是上面提到的是DispatcherServlet中的特殊bean可以自定义配置处理。某种程度上讲HandlerExceptionResolver与你在web应用描述符web.xml文件中能定义的异常映射exception mapping很相像不过它比后者提供了更灵活的方式。比如它能提供异常被抛出时正在执行的是哪个处理器这样的信息。HandlerExceptionResolver 提供resolveException接口public interface HandlerExceptionResolver { ModelAndView resolveException( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);
}在BaseController中使用 ExceptionHandler注解处理异常 ExceptionHandler(Exception.class) public Object exceptionHandler(Exception ex, HttpServletResponse response, HttpServletRequest request) throws IOException { String url ; String msg ex.getMessage(); Object resultModel null; try { if (ex.getClass() HttpRequestMethodNotSupportedException.class) { url admin/common/500; System.out.println(--------毛有找到对应方法---------); } else if (ex.getClass() ParameterException.class) {//自定义的异常 } else if (ex.getClass() UnauthorizedException.class) { url admin/common/unauth; System.out.println(--------毛有权限---------); } String header req.getHeader(X-Requested-With); boolean isAjax XMLHttpRequest.equalsIgnoreCase(header); String method req.getMethod(); boolean isPost POST.equalsIgnoreCase(method); if (isAjax || isPost) { return Message.error(msg); } else { ModelAndView view new ModelAndView(url); view.addObject(error, msg); view.addObject(class, ex.getClass()); view.addObject(method, request.getRequestURI()); return view; } } catch (Exception exception) { logger.error(exception.getMessage(), exception); return resultModel; } finally { logger.error(msg, ex); ex.printStackTrace(); } }在web.xml中处理异常!-- 默认的错误处理页面 --error-page error-code403/error-code location/403.html/location/error-pageerror-page error-code404/error-code location/404.html/location/error-page!-- 仅仅在调试的时候注视掉,在正式部署的时候不能注释 --!-- 这样配置也是可以的表示发生500错误的时候转到500.jsp页面处理。 --error-page error-code500/error-code location/500.html/location /error-page !-- 这样的配置表示如果jsp页面或者servlet发生java.lang.Exception类型当然包含子类的异常就会转到500.jsp页面处理。 --error-page exception-typejava.lang.Exception/exception-type location/500.jsp/location /error-page error-page exception-typejava.lang.Throwable/exception-type location/500.jsp/location /error-page!-- 当error-code和exception-type都配置时exception-type配置的页面优先级高及出现500错误发生异常Exception时会跳转到500.jsp--来一个问题HandlerExceptionResolver和web.xml中配置的error-page会有冲突吗解答如果resolveException返回了ModelAndView会优先根据返回值中的页面来显示。不过resolveException可以返回null此时则展示web.xml中的error-page的500状态码配置的页面。当web.xml中有相应的error-page配置则可以在实现resolveException方法时返回null。API文档中对返回值的解释return a corresponding ModelAndView to forward to, or null for default processing.spring mvc简介与运行原理Spring的模型-视图-控制器MVC框架是围绕一个DispatcherServlet来设计的这个Servlet会把请求分发给各个处理器并支持可配置的处理器映射、视图渲染、本地化、时区与主题渲染等甚至还能支持文件上传。原理.png(1) Http请求客户端请求提交到DispatcherServlet。(2) 寻找处理器由DispatcherServlet控制器查询一个或多个HandlerMapping找到处理请求的Controller。(3) 调用处理器DispatcherServlet将请求提交到Controller。(4)(5)调用业务处理和返回结果Controller调用业务逻辑处理后返回ModelAndView。(6)(7)处理视图映射并返回模型 DispatcherServlet查询一个或多个ViewResoler视图解析器找到ModelAndView指定的视图。(8) Http响应视图负责将结果显示到客户端。主要注解spring mvc注解.pngContextLoaderListener在讲ContextLoaderListener之前首先来了解一下web.xml的作用。一个web中可以没有web.xml文件也就是说web.xml文件并不是web工程必须的。web.xml文件是用来初始化配置信息比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。当你的web工程没用到这些时你可以不用web.xml文件来配置你的Application。当要启动某个web项目时服务器软件或容器如tomcat会第一步加载项目中的web.xml文件通过其中的各种配置来启动项目只有其中配置的各项均无误时项目才能正确启动。web.xml有多项标签在其加载的过程中顺序依次为context-param listener fileter servlet。同类多个节点以出现顺序依次加载web.xml加载过程.png而spring mvc启动过程大致分为两个过程ContextLoaderListener初始化实例化IoC容器并将此容器实例注册到ServletContext中。DispatcherServlet初始化。web.xml配置.png其中ContextLoaderListener监听器它实现了ServletContextListener这个接口在web.xml配置这个监听器启动容器时就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类所以整个加载配置过程由ContextLoader来完成。ContextLoaderListener在web.xml中的配置!-- 配置contextConfigLocation初始化参数 --context-param param-namecontextConfigLocation/param-name param-value/WEB-INF/applicationContext.xml/param-value/context-param!-- 配置ContextLoaderListerner --listener listener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenerServletContextListener 接口有两个方法:contextInitialized,contextDestroyedDispatcherServletSpring MVC框架与其他很多web的MVC框架一样请求驱动所有设计都围绕着一个中央Servlet来展开它负责把所有请求分发到控制器同时提供其他web应用开发所需要的功能。不过Spring的中央处理器DispatcherServlet能做的比这更多。下图展示了Spring Web MVC的DispatcherServlet处理请求的工作流。熟悉设计模式的朋友会发现DispatcherServlet应用的其实就是一个“前端控制器”的设计模式其他很多优秀的web框架也都使用了这个设计模式。流程图spring mvc处理请求的流程.jpg在web.xml中的配置!-- servlet定义 --servlet servlet-namedispatcher/servlet-name servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class load-on-startup1/load-on-startup/servletservlet-mapping servlet-namedispatcher/servlet-name url-pattern//url-pattern/servlet-mapping其中load-on-startup表示启动容器时初始化该Servleturl-pattern表示哪些请求交给Spring Web MVC处理 “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。在Spring MVC中每个DispatcherServlet都持有一个自己的上下文对象WebApplicationContext它又继承了根rootWebApplicationContext对象中已经定义的所有bean。这些继承的bean可以在具体的Servlet实例中被重载在每个Servlet实例中你也可以定义其scope下的新bean。WebApplicationContext继承自ApplicationContext它提供了一些web应用经常需要用到的特性。它与普通的ApplicationContext不同的地方在于它支持主题的解析并且知道它关联到的是哪个servlet它持有一个该ServletContext的引用DispatcherServlet继承结构spring mvc同时提供了很多特殊的注解用于处理请求和渲染视图等。DispatcherServlet初始化的过程中会默认使用这些特殊bean进行配置。如果你想指定使用哪个特定的bean你可以在web应用上下文WebApplicationContext中简单地配置它们。特殊bean.png其中常用的ViewResolver的配置。以jsp作为视图为例!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --bean classorg.springframework.web.servlet.view.InternalResourceViewResolver property nameprefix value/WEB-INF/jsp/ / property namesuffix value.jsp //bean配置上传文件限制MultipartResolver!-- 上传限制 --bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver !-- 上传文件大小限制为31M31*1024*1024 -- property namemaxUploadSize value32505856//beanapplicationContext.xml中的标签applicationContext.xml配置文件标签.png文件上传前面说到DispatcherServlet中有个特殊的Bean叫MultipartResolver可用于限制文件的上传大小等。当解析器MultipartResolver完成处理时请求便会像其他请求一样被正常流程处理。表单form methodpost action/form enctypemultipart/form-datainput typetext namename/input typefile namefile/input typesubmit/
/form控制器RequestMapping(path /form, method RequestMethod.POST) public String handleFormUpload(RequestParam(name) String name, RequestParam(file) MultipartFile file) { if (!file.isEmpty()) { byte[] bytes file.getBytes(); // store the bytes somewhere return redirect:uploadSuccess; } return redirect:uploadFailure;}异常处理先来说下常见的异常处理有几种方式如下图异常处理方式.pngSpring的处理器异常解析器HandlerExceptionResolver接口的实现负责处理各类控制器执行过程中出现的异常。也是上面提到的是DispatcherServlet中的特殊bean可以自定义配置处理。某种程度上讲HandlerExceptionResolver与你在web应用描述符web.xml文件中能定义的异常映射exception mapping很相像不过它比后者提供了更灵活的方式。比如它能提供异常被抛出时正在执行的是哪个处理器这样的信息。HandlerExceptionResolver 提供resolveException接口public interface HandlerExceptionResolver { ModelAndView resolveException( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);
}在BaseController中使用 ExceptionHandler注解处理异常 ExceptionHandler(Exception.class) public Object exceptionHandler(Exception ex, HttpServletResponse response, HttpServletRequest request) throws IOException { String url ; String msg ex.getMessage(); Object resultModel null; try { if (ex.getClass() HttpRequestMethodNotSupportedException.class) { url admin/common/500; System.out.println(--------毛有找到对应方法---------); } else if (ex.getClass() ParameterException.class) {//自定义的异常 } else if (ex.getClass() UnauthorizedException.class) { url admin/common/unauth; System.out.println(--------毛有权限---------); } String header req.getHeader(X-Requested-With); boolean isAjax XMLHttpRequest.equalsIgnoreCase(header); String method req.getMethod(); boolean isPost POST.equalsIgnoreCase(method); if (isAjax || isPost) { return Message.error(msg); } else { ModelAndView view new ModelAndView(url); view.addObject(error, msg); view.addObject(class, ex.getClass()); view.addObject(method, request.getRequestURI()); return view; } } catch (Exception exception) { logger.error(exception.getMessage(), exception); return resultModel; } finally { logger.error(msg, ex); ex.printStackTrace(); } }在web.xml中处理异常!-- 默认的错误处理页面 --error-page error-code403/error-code location/403.html/location/error-pageerror-page error-code404/error-code location/404.html/location/error-page!-- 仅仅在调试的时候注视掉,在正式部署的时候不能注释 --!-- 这样配置也是可以的表示发生500错误的时候转到500.jsp页面处理。 --error-page error-code500/error-code location/500.html/location /error-page !-- 这样的配置表示如果jsp页面或者servlet发生java.lang.Exception类型当然包含子类的异常就会转到500.jsp页面处理。 --error-page exception-typejava.lang.Exception/exception-type location/500.jsp/location /error-page error-page exception-typejava.lang.Throwable/exception-type location/500.jsp/location /error-page!-- 当error-code和exception-type都配置时exception-type配置的页面优先级高及出现500错误发生异常Exception时会跳转到500.jsp--来一个问题HandlerExceptionResolver和web.xml中配置的error-page会有冲突吗解答如果resolveException返回了ModelAndView会优先根据返回值中的页面来显示。不过resolveException可以返回null此时则展示web.xml中的error-page的500状态码配置的页面。当web.xml中有相应的error-page配置则可以在实现resolveException方法时返回null。API文档中对返回值的解释return a corresponding ModelAndView to forward to, or null for default processing.