springmvc十六:视图解析
生活随笔
收集整理的這篇文章主要介紹了
springmvc十六:视图解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?spring.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.atchina"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean> </beans>方法執行后的返回值會作為頁面地址參考,轉發或者重定向到頁面。
視圖解析器可能會進行頁面地址的拼串。
?
forward前綴指定一個轉發操作
/*** forward:轉發到一個頁面* /hello.jsp 轉發當前項目下的hello.jsp* * forward:/hello.jsp, 視圖解析器不會在這個返回值加前后綴* forward:/hello.jsp 一定要加上/,如果不加/就是相對路徑,容易出問題*/@RequestMapping("/hello3")public String hello3(){return "forward:/hello.jsp";}/*** forward:轉發到一個請求*/@RequestMapping("/hello4")public String hello4(){return "forward:/hello3";}重定向redirect
/*** 重定向到hello.jsp頁面 , 視圖解析器不會在這個返回值加前后綴* 轉發: forward: 轉發的路徑* 重定向: redirect:重定向的路徑* /hello.jsp 代表就是從當前項目下開始,SpringMvc會為路徑自動的拼接上項目名 */@RequestMapping("/hello5")public String hello5(){return "redirect:/hello.jsp";}/*** 重定向一個請求*/@RequestMapping("/hello6")public String hello6(){return "redirect:/hello5";}?
視圖解析流程
? 1. 任何方法的返回值,最終都會被包裝成ModelAndView對象。
2. 該方法處理視圖渲染流程,將域中的數據在頁面展示:頁面就是用來渲染模型數據的。
? ? processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); ?// 來到頁面的方法
3. 調用render(mv, request, response);渲染頁面
4. View 和 ViewResolver?
? ViewResolver的作用是根據視圖名(方法的返回值)得到View對象。
public interface ViewResolver {/*** Resolve the given view by name.* <p>Note: To allow for ViewResolver chaining, a ViewResolver should* return {@code null} if a view with the given name is not defined in it.* However, this is not required: Some ViewResolvers will always attempt* to build View objects with the given name, unable to return {@code null}* (rather throwing an exception when View creation failed).* @param viewName name of the view to resolve* @param locale Locale in which to resolve the view.* ViewResolvers that support internationalization should respect this.* @return the View object, or {@code null} if not found* (optional, to allow for ViewResolver chaining)* @throws Exception if the view cannot be resolved* (typically in case of problems creating an actual View object)*/View resolveViewName(String viewName, Locale locale) throws Exception;}5. 怎么根據方法的返回值(視圖名)得到View對象?
? ?所有配置的視圖解析器都會嘗試根據視圖名(方法的返回值)得到View(視圖)對象;如果能得到View對象就返回,得不到就換下一個視圖解析器。
protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale,HttpServletRequest request) throws Exception {for (ViewResolver viewResolver : this.viewResolvers) {// ViewResolver視圖解析器根據方法的返回值,得到一個View對象View view = viewResolver.resolveViewName(viewName, locale);if (view != null) {return view;}}return null;}??
調用View對象的render方法
@Overridepublic void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {if (logger.isTraceEnabled()) {logger.trace("Rendering view with name '" + this.beanName + "' with model " + model +" and static attributes " + this.staticAttributes);}Map<String, Object> mergedModel = createMergedOutputModel(model, request, response);prepareResponse(request, response);// 渲染要給頁面輸出的所有數據renderMergedOutputModel(mergedModel, getRequestToExpose(request), response);}?InternalResourceView類
@Overrideprotected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {// Expose the model object as request attributes.// 將模型中的數據放在請求域中exposeModelAsRequestAttributes(model, request);// Expose helpers as request attributes, if any.exposeHelpers(request);// Determine the path for the request dispatcher.String dispatcherPath = prepareForRendering(request, response);// Obtain a RequestDispatcher for the target resource (typically a JSP).RequestDispatcher rd = getRequestDispatcher(request, dispatcherPath);if (rd == null) {throw new ServletException("Could not get RequestDispatcher for [" + getUrl() +"]: Check that the corresponding file exists within your web application archive!");}// If already included or response already committed, perform include, else forward.if (useInclude(request, response)) {response.setContentType(getContentType());if (logger.isDebugEnabled()) {logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");}rd.include(request, response);}else {// Note: The forwarded resource is supposed to determine the content type itself.if (logger.isDebugEnabled()) {logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'");}rd.forward(request, response);}}? 總結: 視圖解析器只是為了得到視圖對象;視圖對象才能真正的轉發(將模型數據全部放在請求域中)或者重定向到頁面;視圖對象才能真正的渲染視圖。
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的springmvc十六:视图解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springmvc十六:九大组件
- 下一篇: springmvc十七:自定义视图和自定