简体   繁体   中英

Returning a subclass object in jsp form spring mvc

How to return the subclass object to the controller from the jsp view. The page receives the proper animal list with the sub class elements. I am able to display the child class elements but when I try to send it back to the controller I am getting a binding error. This is the mock code for my issue.

   public class Group
    {
    public List<Animal> animals;
    //getters and setters 
    }


    abstract class Animal
    {
    String name;
    //getters and setters 
    }

    class Lion extends animal
    {
    String legs;
    //getters and setters 
    }

My view:

<form:hidden path="groups[${groupssList.index}].animals[${animalsList.index}].name"/>

Exception:

Could not instantiate property type [Animal] to auto-grow nested property path: java.lang.InstantiationException

I think your path value will result in a null reference and Spring will try populate it with a default object. To disable this default behavior, add to (each of) your @Controller class.

@InitBinder
public void initBinder(WebDataBinder binder){
    binder.setAutoGrowNestedPaths(false);
}

You will end up getting a different exception, but it will be more clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM