Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

java - SpringMVC Custom Collection Editor Not Returning Data To Jsp

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Solution to this issue was very simple all of the work was already done in the CustomCollectionEditor. This is important when binding complex data types such as above. There may be other approaches to doing this however i find this to be a very clean and simple approach.

The return statement is very important since it binds to the item attribute of the element in the view. CustomCollectionEditor return a list of objects (victims) The use of the DAO gets the object from the database. This is important since the post only sends the select value not the label, hence we reconstruct the list and resend to the view.

The part of this that i omitted was passing 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*");
...............

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...