日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

SpringMVC知识整理

發(fā)布時(shí)間:2023/12/29 javascript 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC知识整理 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. spring mvc簡(jiǎn)介與運(yùn)行原理

Spring的模型-視圖-控制器(MVC)框架是圍繞一個(gè)DispatcherServlet來(lái)設(shè)計(jì)的,這個(gè)Servlet會(huì)把請(qǐng)求分發(fā)給各個(gè)處理器,并支持可配置的處理器映射、視圖渲染、本地化、時(shí)區(qū)與主題渲染等,甚至還能支持文件上傳。

? ? ? ? ? ?

?

  • (1) Http請(qǐng)求:客戶端請(qǐng)求提交到DispatcherServlet。

  • (2) 尋找處理器:由DispatcherServlet控制器查詢一個(gè)或多個(gè)HandlerMapping,找到處理請(qǐng)求的Controller。

  • (3) 調(diào)用處理器:DispatcherServlet將請(qǐng)求提交到Controller。

  • (4)(5)調(diào)用業(yè)務(wù)處理和返回結(jié)果:Controller調(diào)用業(yè)務(wù)邏輯處理后,返回ModelAndView。

  • (6)(7)處理視圖映射并返回模型: DispatcherServlet查詢一個(gè)或多個(gè)ViewResoler視圖解析器,找到ModelAndView指定的視圖。

  • (8) Http響應(yīng):視圖負(fù)責(zé)將結(jié)果顯示到客戶端。

2.?主要注解

? ? ? ? ?

?3.?ContextLoaderListener

在講ContextLoaderListener之前,首先來(lái)了解一下web.xml的作用。

  • 一個(gè)web中可以沒(méi)有web.xml文件,也就是說(shuō),web.xml文件并不是web工程必須的。web.xml文件是用來(lái)初始化配置信息:比如Welcome頁(yè)面、servlet、servlet-mapping、filter、listener、啟動(dòng)加載級(jí)別等。當(dāng)你的web工程沒(méi)用到這些時(shí),你可以不用web.xml文件來(lái)配置你的Application。

  • 當(dāng)要啟動(dòng)某個(gè)web項(xiàng)目時(shí),服務(wù)器軟件或容器如(tomcat)會(huì)第一步加載項(xiàng)目中的web.xml文件,通過(guò)其中的各種配置來(lái)啟動(dòng)項(xiàng)目,只有其中配置的各項(xiàng)均無(wú)誤時(shí),項(xiàng)目才能正確啟動(dòng)。web.xml有多項(xiàng)標(biāo)簽,在其加載的過(guò)程中順序依次為:context-param >> listener >> fileter >> servlet。(同類多個(gè)節(jié)點(diǎn)以出現(xiàn)順序依次加載)

而spring mvc啟動(dòng)過(guò)程大致分為兩個(gè)過(guò)程:

  • ContextLoaderListener初始化,實(shí)例化IoC容器,并將此容器實(shí)例注冊(cè)到ServletContext中。

  • DispatcherServlet初始化。

其中ContextLoaderListener監(jiān)聽(tīng)器它實(shí)現(xiàn)了ServletContextListener這個(gè)接口,在web.xml配置這個(gè)監(jiān)聽(tīng)器,啟動(dòng)容器時(shí),就會(huì)默認(rèn)執(zhí)行它實(shí)現(xiàn)的方法。在ContextLoaderListener中關(guān)聯(lián)了ContextLoader這個(gè)類,所以整個(gè)加載配置過(guò)程由ContextLoader來(lái)完成。

  • ContextLoaderListener在web.xml中的配置

<!-- 配置contextConfigLocation初始化參數(shù) --> <context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 配置ContextLoaderListerner --> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

ServletContextListener 接口有兩個(gè)方法:contextInitialized,contextDestroyed

4. DispatcherServlet

Spring MVC框架,與其他很多web的MVC框架一樣:請(qǐng)求驅(qū)動(dòng);所有設(shè)計(jì)都圍繞著一個(gè)中央Servlet來(lái)展開(kāi),它負(fù)責(zé)把所有請(qǐng)求分發(fā)到控制器;同時(shí)提供其他web應(yīng)用開(kāi)發(fā)所需要的功能。不過(guò)Spring的中央處理器,DispatcherServlet,能做的比這更多。

  • spring mvc處理請(qǐng)求流程圖

  • 在web.xml中的配置?
<!-- servlet定義 --><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup> </servlet> <servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern> </servlet-mapping>

其中

  • load-on-startup:表示啟動(dòng)容器時(shí)初始化該Servlet;

  • url-pattern:表示哪些請(qǐng)求交給Spring Web MVC處理, “/” 是用來(lái)定義默認(rèn)servlet映射的。也可以如“*.html”表示攔截所有以html為擴(kuò)展名的請(qǐng)求。

在Spring MVC中,每個(gè)DispatcherServlet都持有一個(gè)自己的上下文對(duì)象WebApplicationContext,它又繼承了根(root)WebApplicationContext對(duì)象中已經(jīng)定義的所有bean。這些繼承的bean可以在具體的Servlet實(shí)例中被重載,在每個(gè)Servlet實(shí)例中你也可以定義其scope下的新bean。

WebApplicationContext繼承自ApplicationContext,它提供了一些web應(yīng)用經(jīng)常需要用到的特性。它與普通的ApplicationContext不同的地方在于,它支持主題的解析,并且知道它關(guān)聯(lián)到的是哪個(gè)servlet(它持有一個(gè)該ServletContext的引用)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?DispatcherServlet繼承結(jié)構(gòu)

spring mvc同時(shí)提供了很多特殊的注解,用于處理請(qǐng)求和渲染視圖等。DispatcherServlet初始化的過(guò)程中會(huì)默認(rèn)使用這些特殊bean進(jìn)行配置。如果你想指定使用哪個(gè)特定的bean,你可以在web應(yīng)用上下文WebApplicationContext中簡(jiǎn)單地配置它們。

? ? ? ? ? ? ? ? ? ?

其中,常用的ViewResolver的配置。以jsp作為視圖為例:

<!-- 對(duì)模型視圖名稱的解析,即在模型視圖名稱添加前后綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /> </bean>

?配置上傳文件限制MultipartResolver

<!-- 上傳限制 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 上傳文件大小限制為31M,31*1024*1024 --><property name="maxUploadSize" value="32505856"/> </bean>

5. applicationContext.xml中的標(biāo)簽

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

6. 文件上傳?

前面說(shuō)到DispatcherServlet中有個(gè)特殊的Bean叫MultipartResolver,可用于限制文件的上傳大小等。當(dāng)解析器MultipartResolver完成處理時(shí),請(qǐng)求便會(huì)像其他請(qǐng)求一樣被正常流程處理。

  • 表單:
<form method="post" action="/form" enctype="multipart/form-data"><input type="text" name="name"/><input type="file" name="file"/><input type="submit"/> </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 somewherereturn "redirect:uploadSuccess";}return "redirect:uploadFailure"; }

7. 異常處理

? ? ? ? ? ? ? ? ? ? ? ??

?

Spring的處理器異常解析器HandlerExceptionResolver接口的實(shí)現(xiàn)負(fù)責(zé)處理各類控制器執(zhí)行過(guò)程中出現(xiàn)的異常。也是上面提到的,是DispatcherServlet中的特殊bean,可以自定義配置處理。

某種程度上講,HandlerExceptionResolver與你在web應(yīng)用描述符web.xml文件中能定義的異常映射(exception mapping)很相像,不過(guò)它比后者提供了更靈活的方式。比如它能提供異常被拋出時(shí)正在執(zhí)行的是哪個(gè)處理器這樣的信息。

  • 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("--------沒(méi)有找到對(duì)應(yīng)方法---------");} else if (ex.getClass() == ParameterException.class) {//自定義的異常} else if (ex.getClass() == UnauthorizedException.class) {url = "admin/common/unauth";System.out.println("--------沒(méi)有權(quán)限---------");}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中處理異常
<!-- 默認(rèn)的錯(cuò)誤處理頁(yè)面 --><error-page><error-code>403</error-code><location>/403.html</location></error-page><error-page><error-code>404</error-code><location>/404.html</location></error-page><!-- 僅僅在調(diào)試的時(shí)候注視掉,在正式部署的時(shí)候不能注釋 --><!-- 這樣配置也是可以的,表示發(fā)生500錯(cuò)誤的時(shí)候,轉(zhuǎn)到500.jsp頁(yè)面處理。 --><error-page> <error-code>500</error-code> <location>/500.html</location> </error-page> <!-- 這樣的配置表示如果jsp頁(yè)面或者servlet發(fā)生java.lang.Exception類型(當(dāng)然包含子類)的異常就會(huì)轉(zhuǎn)到500.jsp頁(yè)面處理。 --><error-page> <exception-type>java.lang.Exception</exception-type> <location>/500.jsp</location> </error-page> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/500.jsp</location> </error-page><!-- 當(dāng)error-code和exception-type都配置時(shí),exception-type配置的頁(yè)面優(yōu)先級(jí)高及出現(xiàn)500錯(cuò)誤,發(fā)生異常Exception時(shí)會(huì)跳轉(zhuǎn)到500.jsp-->
  • 來(lái)一個(gè)問(wèn)題:HandlerExceptionResolver和web.xml中配置的error-page會(huì)有沖突嗎?

? ? ?解答:如果resolveException返回了ModelAndView,會(huì)優(yōu)先根據(jù)返回值中的頁(yè)面來(lái)顯示。不過(guò),resolveException可以返回null,此時(shí)則展示web.xml中的error-page的500狀態(tài)碼配置的頁(yè)面。
? ? 當(dāng)web.xml中有相應(yīng)的error-page配置,則可以在實(shí)現(xiàn)resolveException方法時(shí)返回null。

總結(jié)

以上是生活随笔為你收集整理的SpringMVC知识整理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。