I recently encountered a problem where I need to pass in a group of objects in the project. In the past, I have only encountered cases where one object, multiple objects, or a set of arrays were passed in. It is rare to see if a group of objects is passed in. So it feels a bit tricky when you encounter this problem. I thought it would be possible to solve it in this way.
@RequestMapping("save1")public void save1(Plan plan, List<PlanItem> planItems, HttpServletResponse response) { //TODO more... addMessage(response, MESSAGE_TYPE_SUCCESS, null, MESSAGE_SUCCESS);}But later I found out that I was too stupid and naive. This method cannot bind parameters at all. Some people say that spring must sacrifice part of its performance compared to struts. But I have no research on spring and struts, I just use it as a tool. So, I really don't know why. Going far, back to the topic. There are other solutions online. This is how
public class PersonList { private List<Person> person; public List<Person> getPerson() { return person; } public void setPerson(List<Person> person) { this.Person= person; } } In controller:
@RequestMapping(value = "xxx", method = RequestMethod.POST) public void test(PersonList person) { //Transipate person }} The page should be written like this:
<form id="form" action="test/test.do" method="post"><input type="text" name="person[0].username" value="jobs"/><input type="text" name="person[0].age" value="55"/><br/><input type="text" name="person[1].username]" value="jim"/><input type="text" name="person[1].age" value="21"/><br/><input type="submit"><form>
But I tried this method, but it still didn't work. I don't know if I'm too stupid or why.
After many twists and turns, another solution was found. Pass the json string of the list object into the background. After the background receives the parameters. Then convert json to object. The front-end code is exempted, the background code is as follows.
@RequestMapping("save")public void save(Plan plan, String planItemJson, HttpServletResponse response) { List<PlanItem> parseArray = JSON.parseArray(planItemJson, PlanItem.class); //TODO what u want addMessage(response, MESSAGE_TYPE_SUCCESS, null, MESSAGE_SUCCESS);}But there is a small problem with this, that is, the json string will be automatically added with double quotes in the key section in the previous section. When decoding in the background, double quotes are escaped as "";" so you also need to convert "";" to double quotes. That is, the final code is
@RequestMapping("save")public void save(Plan plan, String planItemJson, HttpServletResponse response) { planItemJson = planItemJson.replaceAll("","/""); List<PlanItem> parseArray = JSON.parseArray(planItemJson, PlanItem.class); //TODO what u want addMessage(response, MESSAGE_TYPE_SUCCESS, null, MESSAGE_SUCCESS);}After testing, this method also works for list objects and list objects.
PS: In order to improve program robustness, you can verify json after escaping JSON objects, which avoids errors in json turning to java objects.
Or use a more violent method to perform try catch operations when json turns to list.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.