SpringMVC custom collection editor does not return data in Jsp

I associate the list with several selections in spring, the element does not get its data from the DAO, the data is added from another list of selection options. The user clicks the button and the data is sent to the selection list of several options using jquery.

When the form is placed, data binding is not performed for the element, because its complex data type, so I registered CustomEditor and bound it to @initbinder.

EDITED I updated the code that CollectionEditor now returns the list of citizens back to the view, but I can not get the data in the list to fill in the selected option. I am trying to add items to the list, but jsp still selects null when returning from the server.

Under the code:

CustomCollectionEditor

@InitBinder("crime") protected void initBinder(WebDataBinder binder, HttpServletRequest request, ServletRequestDataBinder victimbinder){ victimbinder.registerCustomEditor(List.class, "victims", new CustomCollectionEditor(List.class){ protected Object convertElement(Object element){ Citizens victims = new Citizens(); String ssNumber = ""; if (element instanceof String){ ssNumber = (String) element; } logger.debug("element is ;" +element); try { int socialSecurityNumber = Integer.parseInt(ssNumber); victims = citizenManager.getCitizen(socialSecurityNumber); } catch (NumberFormatException e) { logger.error(e.getMessage()); } catch (Exception e) { logger.error(e.getMessage()); } return victims; } }); 

Jsp that populates from DAO in the controller

It contains the filled form DAO class, when the button is pressed, it takes data from the list, adds it to another list, under which it is attached to the POJO

 <label>Victims List</label><buttonid="addVictimBtn">/button> <form:select path="" id="dbvictims" title="Victims Of Crime" class="victimLst"> <form:options items="${dbvictims.dbvictimList}" itemValue="socialSecurityNumber" itemLabel="name"/> </form:select> 

Jsp select POJO bound item

 <label>Victims In Crime</label><button id="removeVictimBtn">-</button> <form:select path="victims" id="victims" title="Victims Of Crime" multiple="multiple" class="victimLst"> <form:options items="${victimList}" itemValue="socialSecurityNumber" itemLabel="name"/> </form:select><form:errors path="victims" class="errors" /> 
+2
source share
1 answer

The solution to this problem was very simple, all the work was already done in CustomCollectionEditor . This is important when linking complex data types, such as above. There may be other approaches to this, however, I believe that this is a very simple and simple approach.

The return statement is very important because it is associated with the attribute of an element element in the view. CustomCollectionEditor returns a list of objects (victims). Using DAO gets an object from the database. This is important because the message sends only the select value, not the label, so we restore the list and resubmit it to the view.

The part of this that I skipped about passes the list object from the controller back to the view.

controller

 @RequestMapping(value="save.htm", method = RequestMethod.POST) public ModelAndView handleSave(@Valid @ModelAttribute Crime crime, BindingResult result, ModelMap m, Model model) throws Exception { if(result.hasErrors()){ model.addAttribute("victimList",crime.getVictims()); return new ModelAndView("*Your View*"); ............... 
+2
source

Source: https://habr.com/ru/post/1469322/


All Articles