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