日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

ModelAndView解析

系統(tǒng) 2512 0

?查看spring的幫助文檔得到下面信息:

org.springframework.web.servlet
Class ModelAndView

      
        
          
            java.lang.Object
          
        
      
      extended by 
      
        org.springframework.web.servlet.ModelAndView
      
    

          public class 
          
            ModelAndView
          
          
extends Object
      
        
      
    

Holder for both Model and View in the web MVC framework. Note that these are entirely distinct. This class merely holds both to make it possible for a controller to return both model and view in a single return value.

Class to represent a model and view returned by a handler used by a DispatcherServlet. The view can take the form of a reference to a View object, or a String view name which will need to be resolved by a ViewResolver object. The model is a Map, allowing the use of multiple data objects keyed by name.

Author:
Rod Johnson, Juergen Hoeller
See Also:
DispatcherServlet , ViewResolver , HandlerAdapter.handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object) , Controller.handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

Constructor Summary
ModelAndView ()
??????????Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.
ModelAndView ( String ?viewName)
??????????Convenient constructor when there is no model data to expose.
ModelAndView ( String ?viewName, Map ?model)
??????????Creates new ModelAndView given a view name and a model.
ModelAndView ( String ?viewName, String ?modelName, Object ?modelObject)
??????????Convenient constructor to take a single model object.
ModelAndView ( View ?view)
??????????Convenient constructor when there is no model data to expose.
ModelAndView ( View ?view, Map ?model)
??????????Creates new ModelAndView given a View object and a model.
ModelAndView ( View ?view, String ?modelName, Object ?modelObject)
??????????Convenient constructor to take a single model object.
?
Method Summary
? ModelAndView addAllObjects ( Map ?modelMap)
??????????Add all entries contained in the provided map to the model.
? ModelAndView addObject ( String ?modelName, Object ?modelObject)
??????????Add an object to the model.
?void clear ()
??????????Clear the state of this ModelAndView object.
? Map getModel ()
??????????Return the model map.
protected ? Map getModelInternal ()
??????????Return the model map.
? View getView ()
??????????Return the View object, or null if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.
? String getViewName ()
??????????Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if we are using a View object.
?boolean isEmpty ()
??????????Return whether this ModelAndView object is empty, i.e. whether it does not hold any view and does not contain a model.
?boolean isReference ()
??????????Return whether we use a view reference, i.e. true if the view has been specified via a name to be resolved by the DispatcherServlet via a ViewResolver.
?void setView ( View ?view)
??????????Set a View object for this ModelAndView.
?void setViewName ( String ?viewName)
??????????Set a view name for this ModelAndView, to be resolved by the DispatcherServlet via a ViewResolver.
? String toString ()
??????????Return diagnostic information about this model and view.

?

?

?收集自其它網(wǎng)頁:

?

ModelAndView 類別就如其名稱所看到的,是代表了Spring Web MVC程式中呈現(xiàn)畫面時所使用Model資料物件與View資料物件,因為Java程式中一次僅僅能返回一個物件,所以ModelAndView的作用封裝這兩個物件,以方便您一次返回Model與View這兩個物件。

最簡單的ModelAndView是持有View的名稱返回,之后View名稱被View resolver,也就是實作org.springframework.web.servlet.View介面的實例解析,比如 InternalResourceView或JstlView等等,最簡單的ModelAndView建構(gòu)方式例如以下:

ModelAndView(String viewName)


假設(shè)您要返回呈現(xiàn)畫面時所需的Model資料物件,則能夠使用Map物件來收集呈現(xiàn)資料時所需的資料,然后在建構(gòu)ModelAndView作為建構(gòu)時的引數(shù),您能夠使用以下這個版本號的ModelAndView建構(gòu)方法:

ModelAndView(String viewName, Map model)


Map物件中設(shè)定好鍵(Key)與值(Value),之后能夠在要呈現(xiàn)的畫面中取出加以顯示(比如在JSP網(wǎng)頁中),假設(shè)您要返回一個Model資料物件并指定Model的名稱,則能夠使用以下這個ModelAndView版本號:

ModelAndView(String viewName, String modelName, Object modelObject)


藉由modelName,您能夠在要View的實作類別中取出Model資料物件,并依據(jù)View所使用的技術(shù)來加以顯示(可能是JSP網(wǎng)頁、Pdf等呈現(xiàn)技術(shù))。

ModelAndView類別也提供實作View介面的物件來作為建構(gòu)時的引數(shù),下面是三個可用的建構(gòu)方法版本號:

ModelAndView(View view)
ModelAndView(View view, Map model)
ModelAndView(View view, String modelName, Object modelObject)


一個實作View的實例例是org.springframework.web.servlet.view.RedirectView, ModelAndView預(yù)設(shè)是使用轉(zhuǎn)發(fā)(Forward)方式來完畢請求的結(jié)果畫面,使用RedirectView的話,則會使用又一次導(dǎo)向(Redirect)將請求重導(dǎo)至指定的結(jié)果畫面位置,以呈現(xiàn)請求的結(jié)果,比如:

...
public ModelAndView handleRequest(....) ... {
??? ...
??? return new ModelAndView(
????????????????????? new RedirectView(this.getViewPage()));
}
...


在這邊,viewPage所設(shè)定的位址要是從伺服器網(wǎng)頁根文件夾開始指定,而不是Web應(yīng)用程式的根文件夾,所以您的getViewPage()傳回的位址必須像是/FirstSpringMVC/pages/index.htm這種位址,當(dāng)中FirstSpringMVC是您的Web應(yīng)用程式文件夾。

使用轉(zhuǎn)發(fā)(Forward)方式的話,網(wǎng)址列上并不會出現(xiàn)被轉(zhuǎn)發(fā)的目標(biāo)位址,并且轉(zhuǎn)發(fā)方式是在Web應(yīng)用程式之內(nèi)進(jìn)行,能夠訪問Web應(yīng)用程式所設(shè)定的內(nèi)部文件夾,像是WEB-INF文件夾,因而您能夠?qū)⒁恍┮毓艽嫒〉馁Y源放到WEB-INF下,如此使用者就無法直接請求這些資源,而必須透過 DispatcherServlet與Controller的處理與控管,才干夠取得這些資源,轉(zhuǎn)發(fā)方式僅僅能在Web應(yīng)用程式中進(jìn)行,不能指定至其他的 Web應(yīng)用程式位址。

使用又一次導(dǎo)向(Redirect)的話,Web應(yīng)用程式會要求client瀏覽器又一次發(fā)出一個所指定的請求位址,也就是實際上相當(dāng)于client又一次連接至一個所指定的位址,因此瀏覽器的位址列上會出現(xiàn)被又一次導(dǎo)向的資訊,又一次導(dǎo)向的請求是由瀏覽器發(fā)出,所以不能訪問Web應(yīng)用程式中的隱藏文件夾,像是WEB-INF,又一次導(dǎo)向是又一次要求一個網(wǎng)頁,所以能夠指定至其他的Web應(yīng)用程式位址。

?

?DispatcherServlet會依據(jù)傳回的ModelAndView來解析View名稱,并處理給予的Model。

?

?View名稱的解析是托付給實作org.springframework.web.servlet.ViewResolver介面的實例,ViewResolver介面定義例如以下:

?public interface ViewResolver { public view resolveViewName( String , Locale locale) throws ServletException;? }

?

ViewResolver的一個實例是InternalResourceViewResolver,名稱解析完之后,實際的View繪制與Model轉(zhuǎn)換處理是交給實作org.springframework.web.servlet.View的實例,View介面例如以下:

?

?public interface View { public void render(Map model, HttpServletResquest resquest, HttpServletResponse response) throws ServletException, IOException; }

View的實作之前用過org.springframework.web.servlet.view.InternalResourceView,另外也還有JstlView、TilesView、VelocityView等等的實作,分別進(jìn)行不同的表現(xiàn)展處理。

?

?

ModelAndView解析


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 拉孜县| 彩票| 乌拉特前旗| 吉林省| 康乐县| 如皋市| 友谊县| 柏乡县| 苍南县| 乃东县| 台南县| 信阳市| 临猗县| 常宁市| 巩留县| 六安市| 赞皇县| 乐安县| 扶风县| 喀喇| 徐闻县| 拉萨市| 射阳县| 徐汇区| 西乌| 苏尼特左旗| 黄骅市| 澎湖县| 盐源县| 区。| 邛崃市| 封开县| 手游| 股票| 石泉县| 鄂伦春自治旗| 江油市| 中牟县| 策勒县| 兴宁市| 台东县|