Solution to automatically bind list with spring MVC parameters

Recently, I encountered a problem that I need to pass in a group of objects in the project. In the past, only one object, multiple objects or a group of arrays have been passed in. It is rare to pass in a set of objects. So I feel a little tricky when I encounter this problem. I thought it could be solved in this way.

But then I found myself too stupid and naive. This method can't bind parameters at all. Some people say that when spring pursues performance compared with struts, it must sacrifice some things. But I don't have any research on spring and struts. I just use them as a tool. So, I really don't know why. Let's go further and get back to the point. There are other solutions online. This is the way

In controller:

The page should read:

But I tried this method, but it still didn't work. I don't know if I'm stupid or why.

After several twists and turns, another solution was found. Pass the JSON string of the list object into the background. After receiving the parameters in the background. Then turn JSON into an object. The front-end code is exempted, and the background code is as follows.

However, there is a small problem, that is, the JSON string will be automatically double quoted in the key by the preceding paragraph. When background decoding is caused, double quotation marks are escaped as ""; " Therefore, you also need to add ";" Convert to double quotation marks. That is, the final code is

After testing, this method also works for the list object contained in the list object.

PS: in order to improve program robustness, you can verify JSON after escaping JSON objects, so as to avoid errors in converting JSON to Java objects.

Or use a more violent method to try catch when JSON is converted to list.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>