Modelandview objects in SPRINGMVC with the view parser

Source: Internet
Author: User

Abstract: Spring MVC is an environment in which spring MVC parses, parses, and provides a view, as a response, based on the return value of the processing method in the controller (or you call it handler). Labeled @controller processor, in fact, essentially a pojo, you labeled @controller, I will thought highly you a glance.

Spring MVC in this environment, spring MVC will parse, based on the return value of the processing method in the controller (or you call it handler), and provide a view after parsing, as a response.
Labeled @controller processor, in fact, essentially a pojo, you labeled @controller, I will thought highly you a glance. But your form is a Java code file.
You as a Java soil file, the return value of your inside processing method, which is the return statement, returns a thing. This can be a string or a Modelandview object. This is the whole work of the method that marked the @controller.

Next, the spring container (or the Spring MVC container) needs to be followed by the return value you've thrown, regardless of whether your return value is string or Modelandview, and I, as a container, I'm all encapsulated as a Modelandview object. Then, I, part of the spring container, the view parser, begins to work.

The English name of the view parser is called Viewresolver, which is the first spring-defined interface, and what is the function of the view parser in your spring container. Depending on what kind of implementation class you configured for your own spring container, the Spring view parser.

Look at one of the graphs we've seen before:
This is the default configuration in the jar of spring MVC.

Of course your spring project can also overwrite the above configuration in the configuration file (I did not replace the default internalresourceviewresolver with another view parser):

/**     *@return Login Verification */ @RequestMapping ( "/dologin") public modelandview dologin ( HttpServletRequest request, user user) {User US1 = Uss.getuserbyname (User.getsrname ()); Modelandview Mav = new Modelandview (); Mav.setviewname (if (US1 = null) {mav.addobject ( " ErrorMsg ", " user name does not exist "); } else if (!us1.getsrpwd (). Equals (User.getsrpwd ())) { Mav.addobject ( "errormsg",  "user password is incorrect");} else {} return Mav;}       

The method return value in @Controller is ultimately modelandview, and we need to figure out two things:
What is 1.ModelAndView?
2. What exactly does the view parser do to return to the view we need?

We should first see what Modelandview is all about:
Modelandview is the standard class in spring and is completely the object of spring's own encapsulation. This object is described in the Spring API as such:
public class Modelandview extends Java.lang.Object

Holder for both Model and View in the Web MVC framework. Note that these is 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.

Represents a model and view returned by a handler, to is resolved by a dispatcherservlet. The view can take the form of a String view name which would need to being resolved by a Viewresolver object; Alternatively a View object can be specified directly. The model is a MAP, allowing the use of multiple objects keyed by name.

Use the words to explain what Modelandview is for, Modelandview contains two parts: A view and a model
View is determined by the Setviewname () method, decide where to get viewresolver to find the view file and find out which JSP file it is;
Model is determined by the AddObject () method, its essence is Java's hashmap, the key value pair;
The function of employing words to explain modelandview is that the view is responsible for rendering the model, allowing you to find the JSP representing the view, using this JSP to render the data in the model.

Look at the spring Source:
API provided by Spring official website

Go to this path for a look:

This means that the core members of the Modelandview object are objects and Modelmap
Where Modelmap is also the object that spring defines itself.

The essence of Modelmap is the standard container for Java: Linkedhashmap
Attribute members We've got it figured out, here are the method members:
Setviewname () method and AddObject () method

   public void Setviewname ( @Nullable String viewName)  {this.view = ViewName; } public modelandview addobject (String attributename, Object attributevalue) {GetModelMap (). AddAttribute (AttributeName, AttributeValue); return this;} public modelandview addobject (Object attributevalue) {Getmodelmap (). AddAttribute ( AttributeValue); return this;} public modelmap Getmodelmap () {if ( this.model = = null) {this.model = New Modelmap (); } return this.model;}       

In other words, the Modelandview object has no mystery, and the core is Object\linkedhashmap, which is completely Java's standard container (object).
In other words, the key is not the Modelandview object, but the core part of the spring container, the view parser.

So how does the view parser work?
You know that the Viewresolver implementation class you're using is internalresourceviewresolver, so you should take a closer look at the details of this section of the Spring API:
https://docs.spring.io/spring/docs/5.0.1.RELEASE/javadoc-api/

First Internalresourceviewresolver extends (inherit) the Urlbasedviewresolver;
Then, by the way, placing the JSP file for display (view) in the Web-inf folder is a safe way to access these jsps directly through a URL, which can only be accessed through the Controller Java class.

So we went on to see Urlbasedviewresolver.

I think the explanatory text in spring's official API is enough to solve all the confusion: that's the argument in the Modelandview object's Method Setviewname (), which looks like an argument to a normal string, which format should I use? What should I write? There has been a verdict.

As a special feature, redirect URLs can be specified via the "redirect:" prefix. e.g.: "Redirect:myAction.do" would trigger a redirect to the given URL, rather than resolution as standard view name. This is the typically used for redirecting to a controller URL after finishing a form workflow.

Furthermore, forward URLs can be specified via the "forward:" prefix. e.g.: "Forward:myAction.do" would trigger a forward to the given URL, rather than resolution as standard view name. This is the typically used for controller URLs; It is not supposed to being used for JSP urls-use logical view names there

Modelandview objects in SPRINGMVC with the view parser

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.