简体   繁体   English

以jsp形式以Spring MVC返回子类对象

[英]Returning a subclass object in jsp form spring mvc

How to return the subclass object to the controller from the jsp view. 如何从jsp视图将子类对象返回给控制器。 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. 我认为您的path值将导致null引用,Spring会尝试使用默认对象填充它。 To disable this default behavior, add to (each of) your @Controller class. 若要禁用此默认行为,请添加到@Controller类(每个)中。

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

You will end up getting a different exception, but it will be more clear. 您最终将得到一个不同的异常,但是它将更加清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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