Spring MVC Modelandview

Source: Internet
Author: User
Tags constructor locale

There are 7 ways to construct Modelandview. But they are all interlinked. The parameterless constructor is used here to illustrate how to construct an Modelandview instance.
The Modelandview category, as its name implies, is an object of model and view in the MVC Web program, but it is only convenient for you to return the two objects at once Holder,model and view are still separate concepts. The simplest modelandview is to hold the name of the view returned, after which the view name is parsed by the view resolver, which is the instance of the Org.springframework.web.servlet.View interface, for example Internalresourceview or Jstlview et cetera: Modelandview (String viewName)

If you want to return to the model object, you can use MAP to collect the model objects and then set it to Modelandview, using this version of Modelandview:modelandview (String viewName, map Model
The Map object is set with the key and value values, which can then be removed from the view, and if you just want to return a model object, you can use the following Modelandview version: Modelandview (string viewName, String ModelName, Object Modelobject)
With ModelName, you can remove the model and display it in the view.
The Modelandview category provides objects that implement the view interface as parameters for view:

Modelandview (View view)

Modelandview (view view, Map model)

Modelandview (view view, String modelname, Object modelobject)


An example is the Org.springframework.web.servlet.view.redirectview,modelandview preset is to use forward to forward the request result to the view, using Redirectview, The request is redirected to the view using redirect, for example:

...

Public Modelandview handlerequest (...) {

....

return new Modelandview (New Redirectview (This.getviewpage ()));

}

....


Here, ViewPage's address is specified from the root of the server's Web page, not the root of the Web application, so your getviewpage () must return the address as if it were/springapp/pages/ Index.htm such an address, where Springapp is your Web application directory.
With forward, the forwarded destination address does not appear on the URL column, and forward is within the Web application and can access the Web application's hidden directory, such as Web-inf, but forward can only be done in a Web application, cannot be specified to a different Web application address.
Using redirect, the client browser is required to resend a specified request address, so the URL column will appear on the redirected directory address, the redirect request is issued by the browser, so the Web application can not access the hidden directory, such as Web-inf, but re-request a Web page , you can specify to other Web application addresses.
Dispatcherservlet will parse the view name based on the returned Modelandview and process the given model. The resolution of the view name is a delegate to an instance of the implementation Org.springframework.web.servlet.ViewResolver interface, the Viewresolver interface is defined as follows:

Public interface Viewresolver {

public view Resolveviewname (String, locale locale) throws servletexception;

}


An example of Viewresolver is Internalresourceviewresolver, after the name is resolved, the actual view drawing and the model turn The swap is given to the actual instance of the Org.springframework.web.servlet.View, and the View interface is as follows:

Public interface View {

public void render (MAP model, httpservletresquest resquest, httpservletresponse response) throws Servletexception, IOException;

}


The implementation of the view was preceded by Org.springframework.web.servlet.view.InternalResourceView, as well as Jstlview, Tilesview, Velocityview, etc. , respectively, to carry out different performance exhibition processing.


Modelandview () This construction method constructs a modelandview that cannot be used directly, should not specify a view for it, and does not have a corresponding model object. Of course, the model object is not required, but the view does have to be.

Examples constructed with this constructor are used to add view settings and Model objects later.

There are two ways to set the view for a Modelandview instance: Setviewname (String viewName) and Setview (view view). The former uses viewname, the latter using a pre-constructed view object. One of the former is more commonly used. In fact, view is an interface, not a concrete class that can be constructed, and we can only get instances of the view in other ways. For viewname, it can be either a JSP name or a tiles-defined name, depending on how the Viewnameresolver is used to understand the view name.

How to get an instance of view to study later.

And how to set model for Modelandview instance is more complicated. There are three ways to use:

AddObject (Object modelobject)

AddObject (String modelname, Object modelobject)

Addallobjects (Map modelmap)

Modelandview

You can receive objects of type object, Modelandview it as one of its many model. You must specify a name when using objects of type object. Modelandview can also receive objects with no obvious name, because Modelandview will invoke the. Getvariablename () method of spring's own defined conventions class to generate a name for the model. Clearly, the name is a must for model.

Conventions.getvariablename () The rule to generate a name is to use the lowercase mode of the object's class name as the model name. When the model is a collection or an array, use the class name of the first element of the set plus s to make the model name.

Modelandview can also accept objects of the map type, modelandview the elements in the map as model, rather than the map itself as model. However, other collection classes can use themselves as model objects.

In fact, Modelandview's support for model comes from the class Modelmap, which inherits from HashMap.

A complete example:

Public Modelandview handlerequestinternal (
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Modelandview Mav = new Modelandview ("Hello");//Instantiate the Modelandview instance of a view
Mav.addobject ("message", "Hello world!"); /Add a Model object with a name
return MAV;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.