The difference between Model,modelmap and Modelandview in spring

Source: Internet
Author: User
1. Scenario Analysis


In many real-world project requirements, the backend needs to return directly to the front-end data from the control layer, when the model family comes in handy. 


2. Three differences



①model



Model is an interface whose implementation class is Extendedmodelmap, inheriting the Modelmap class


public class Extendedmodelmap extends Modelmap implements Model


②modelmap



Modelmap inherits the Linkedhashmap,spring framework to automatically create an instance and act as the controller's entry, without the user having to create


public class Modelmap extends linkedhashmap<string,object>


③modelandview



As the name implies, Modelandview refers to the collection of models and views, including both the model and the view, and the Modelandview instances are manually created by the developers themselves, which is one of the main differences with Modelmap;

  3. Example Description



Preparation: The Springboot project integrates the Velocity template engine and will not be a partner to see the author Velocity Integrated blog: http://blog.csdn.net/zhangxing52077/article/details/ 73194948, do not repeat.



① The use of model



Spring automatically creates an instance of the model and acts as an entry for the controller



@RequestMapping ("Hello") public
String testvelocity (Model model,string name) {
    Model.addattribute ("name", name);
    return "Hello";
}








②model is similar to model usage



@RequestMapping ("Hello") public
String testvelocity (modelmap model,string name) {
    Model.addattribute ("name") , name);
    return "Hello";
}


It works the same as the model.



The use of ③modelandview



@RequestMapping ("model") public
Modelandview Testmodel (String name) {
    //build Modelandview instance and set the jump address
    Modelandview view = new Modelandview ("test");
    Place the data in the Modelandview object view, the second parameter can be any Java type
    view.addobject ("name", name);
    Returns Modelandview Object view
    return view;
}


Set the view jump address



Modelandview view = new Modelandview ("test");


Of course, you can also set the jump address



Modelandview view = new Modelandview ();
View.setviewname ("test");


The test indicates that the Test.html,springboot default template file in templates is generally resources/templates




Set the model of the view



View.addobject ("name", "Zhangxing,hello");



This jumps to the front page, and the value is passed over, stone. All right, I'm Zhang Xing Zhaoyuan. Welcome to the BO Master Technology Exchange Group, Group No.: 313145288


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.