Implementation method of data transmission at front and back ends of spring MVC

This article mainly introduces how to transfer data at the front and back ends in spring MVC. The details are as follows:

Back end - > front end

In spring MVC, this is mainly used to transfer data from the back end to the front end through the model. The general writing method is:

First, you need to define a model, then add properties for the model, bind data, and finally add the model to the view. The above steps may not be followed during actual development.

Do not specify a view name

The view parser will infer the view name according to the request path / index, and the correct view name index can still be obtained by removing / index. At this time, it is written as follows:

Do not use model

Because the model itself inherits from the LinkedHashMap class (the model itself is just an interface class, specifically, the class of the instantiated model inherits from LinkedHashMap), the data can be stored in the map and then transmitted to the front end. At this time, it is written as follows:

Do not specify model properties

When the key filled with data in the model is not specified, spring MVC will infer according to the value type and return the alternative name of the key. At this time, it is written as follows:

The results of the above methods are consistent. A data corresponding to the key will be stored in the model, and then the model will be transferred to the view. The view can directly obtain the corresponding data according to the key.

Front end - > back end

Spring MVC supports multiple ways to transfer data from the front end to the back end.

Query parameters

Essentially, it is an RPC request with parameters initiated through HTTP. The form of the request is "/ AA? Name = deyken", and the back-end processing form is:

Path variable

Directly request resources in the form of "/ AA / deyken", and the back-end processing form is:

The second method is recommended in practical use.

form

In spring MVC, the front-end to back-end transmission of form data is also supported. Taking user login as an example, the form form is:

The backend only needs to receive the transmitted variables in the method parameter list without specifying query parameters or path variables:

If the user class has been defined:

Then spring MVC will automatically encapsulate the data transmitted from the form into a user object. At this time, the back-end method can be written as follows:

Postscript

The above only describes several front-end and back-end data interaction methods in spring MVC, but in order to separate front-end and back-end development in actual projects, the above methods are generally not applicable. In the next article, I will introduce the specific method of reading back-end data from the front end using Ajax.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>