gpt4 book ai didi

java - spring mvc以jsp形式返回子类对象

转载 作者:行者123 更新时间:2023-12-01 04:15:22 24 4
gpt4 key购买 nike

如何从jsp View 返回子类对象到 Controller 。该页面接收带有子类元素的正确动物列表。我能够显示子类元素,但是当我尝试将其发送回 Controller 时,我收到绑定(bind)错误。这是我的问题的模拟代码。

   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
}

我的看法:

<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

最佳答案

我认为您的 path 值将导致 null 引用,Spring 将尝试使用默认对象填充它。要禁用此默认行为,请添加到(每个)@Controller 类。

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

您最终会得到一个不同的异常,但它会更清晰。

关于java - spring mvc以jsp形式返回子类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19499020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com