I am binding a multi select list in spring the item does not get its data from the DAO the data is added from another select option list. The user clicks a button and the data is sent to the multi select option list using jquery.
When the form is posted databinding does not happen for the item since its a complex data type so i registered a CustomEditor and attached it to the @initbinder.
EDITED
I have updated the code the CollectionEditor is now returning a list of citizens back to the view however i am unable to get the data in the list to fill the select option. I am trying to add elements to the list however the jsp still selects remain null when return form the server.
Under is 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 is filled from DAO in controller
This contains data filled form DAO class when the button is clicked it takes the data from the list on appends it to the other list under which is bind 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 item that is bind to POJO
<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" />
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…